How to use Excel file from visual studio project resource - c#

I add an excel file to my project resource, and I would like to open it, but method woorkobook.open() take only path string as parameter :-(
What is the proper way to open and use those excel files from resource?

Office applications are before the time of .NET Streams. The applications only work with physical files. You must copy the resource to a physical file or use a third-party component.
Dim sPath As String = My.Computer.FileSystem.GetTempFileName
My.Computer.FileSystem.WriteAllBytes(sPath, My.Resources.us, False)

in VS, if you set the property "Copy to Output Directory" to "Copy always", on the excel file you've added to your project, you can then directly use the name of your file as the value of the path.

Related

How to create text file in .NET project folder, not in bin/Debug folder where is by default

I have a very simple .NET console application in Visual Studio. I am trying to write some words into a text file.
using (StreamWriter file = File.AppendText("log1.txt"))
{
file.WriteLine("Hello from the text file");
}
If the file does not exist, the application creates it in the autogenerated folder bin/Debug.
Is there a way to create this file in the project's directory, where I have .csproj file?
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes, but this can only be done while you are working on your project. Once you are done developing it and try to publish it you won't have access to the location where you have .csproj file, because after publishing you can install it on any PC and it wont have the project you are working on.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug?
No, I assume by real-world applications in your context you mean a published project '.exe' that you can run on any PC. Windows provides you three Data folders that you should use when writing your program so that it works smoothly after publishing:
User Data
Roaming User Data
All User Data
You can acess the above folders in .NET application using the Environment.SpecialFolder:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
As per your given code, try this :
var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"log1.txt");
using (StreamWriter file = File.AppendText(fileName))
{
file.WriteLine("Hello from the text file");
}
This way you will be able to publish your program and it will still work smoothly without hard-coding the path as you were doing previously.
That's why .NET creates them there firstly?
If you don't specify a complete path, and just the file name .NET looks into the working directory of the executable, which in this case is bin/Debug
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes. As explained here (second answer) you can use the post-build event to write down the value of $(ProjectDir) in a text file (using command echo $(ProjectDir) > ..\..\projectdir.txt). This macro contains the directory of your .csproj. This command will create the file projectdir.txt with your project directory after a build process so you read this file contents in your code and use what is inside it to pass to File.AppendText as the base directory to create your file log1.txt.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
That depends on what you want to do. In your case the code creates the file at bin/Debug because that is where your executable are being executed. When you omit the full path to File.AppendText and just pass "log1.txt" as argument, it will create the file in the same folder as the executable are at. If you want a different folder you should specify the folder here (e.g. File.AppendText("C:/log1.txt") will create the file at C:/.
You can create the text file in the root of your project and use copy always to have them in the same place as your executable. If this is just a readonly text file then it's OK because windows doesn't allow you to modify the files reside in Programs folder in OS drive.
If you want your code to modify these text file then you need to put them in appdata folder. In real world example I did this on many project. All the database work my winforms, WPF application need goes in AppData folder.

Read any file and packed file in c# winform

I have a problem.
I have a text file "hello.pdf"
I put the file to the resource of the project and read the file
string filename = #"C:\Users\vivio\Documents\Visual Studio 2015\Projects\STool\STool\Resources\hello.pdf";
System.Diagnostics.Process.Start(filename);
I have a question: If I want packet STool to STool.exe, and then install this packet in the (D:)disk then I think I will not read file hello.pdf because in the code I set pathfile is "C:\Users\vivio\Documents\Visual Studio 2015\Projects\STool\STool\Resources\hello.pdf"
Help me to resolve this problem, thanks all
The following link is same with yours, maybe it would be helpful.
How to read embedded resource text file
Or this one.
How to open a PDF file that is also a project resource?
First, copy the file to your project directory and include it in your project. In the file's properties, set the Copy to Output Directory to Copy Always. Now it will always copy to the build directory when you compile.
It will be saved to Directory.GetCurrentDirectory(), so to reference the file you will always be able to use:
string filename = Directory.GetCurrentDirectory() + "\\Resources\\hello.pdf";

c# Excel AddIn, find content file path when excuting

I am using VSTO to build an Excel AddIn. In this project, I also added some python .py scripts to do some data manipualtion.
The whole project is a C# project, all the .py files are taken as content files of the project, in detail, what I did is setting Properities-Build Action as 'Content', setting Properities-Copy to Output Directory as 'Copy always'.
However, after publish with clickonce, when executing, I couldn't find where the content files are. I already tried like: Application.StartupPath, but did't work. I really need to find the content files path and step into them.
Thanks in advance to anyone who take time to see my question.
Publish page of an office add-in project doesn't have an Application Files button which means you can not include some files in the click once installer in the way that you do it for applications. As an option, you can put your files as embedded resources and then at startup of the add-in, extract them from resources and copy them to add-in output directory.
To do so, you can add your file to Resources.resx and then at StartUp of your add-in, extract the file from resources and save it to the deployment directory and use it.
var assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
var assemblyFolder = System.IO.Path.GetDirectoryName(assemblyLocation);
var file = System.IO.Path.Combine(assemblyFolder , "test.py");
if (!System.IO.File.Exists(file))
System.IO.File.WriteAllBytes(file, Properties.Resources.test);
Now the file is in the path specified in file variable.

relative path using c#

I am using c#. In my project I am having a xml folder in which i have an xml file say "file.xml"..
I want to use the file in my project. I want to take that file from the current project itself,for that I am giving the path as:
xmlDoc.Load(#"..\xml\file.xml");
but it is not taking the file.
It is showing some "C:" path..
how can I retrive this file from project itself.
You should set the Copy to Output Directory property on the file in the Solution Explorer to gocpy the file to the folder with your EXE.
You can then write
xmlDoc.Load(Path.Combine(typeof(MyClass).Assembly, "file.xml"));
This uses the actual location of the EXE file and will work regardless of the current directory.
EDIT: In ASP.Net, you should put your file in the App_Data folder (which is not publicly accessible), then write
xmlDoc.Load(Server.MapPath("~/App_Data/file.xml"));
You should set the Copy to Output Directory to "copy if newer" and you can then use:
Path.Combine(Application.StartupPath, "file.xml");
Path.Combine(typeof(MyClass).Assembly.Location.ToString(), "file.xml")

Text file to store data

I want to persist some data into a text file in my C# application. I added the text file to the root of my project.
How do I access it now? Will this file be embedded with my exe or what?
First, make sure that you right click the file and select "Copy to Output Directory".
Second, the file will not be embedded inside of your Executable. It will be a normal *.txt file alongside your *.exe and you would access it as such:
StreamWriter sw = null;
FileInfo fi = new FileInfo(Path.Combine(Application.StartupPath, "filename.txt"));
if(fi.Exists)
sw = new StreamWriter(fi.Open(FileMode.Open));
You need to set the file to copy to output directory.
You can access the path relatively ("file.txt", or ".file.txt")
It will not be embedded with your exe.
Read this for help on opening a file.
It will be in the same directory as your exe by default, you can also provide a specific path where it can be dropped to via the construct of your StreamWriter or w/e class you are using.
It sounds like your text file will contain "settings" for your application, and if you want to embed these settings then just use the actual Settings built into the project. Properties>Settings. MSDN: Settings
There's also the excellent Nini project (http://nini.sourceforge.net/manual.php) which makes it easy to access (and write) simple settings files of various flavors and to optionally combine them with command line parameters.

Categories

Resources