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";
Related
I'm trying to load and save an xml file called Modules.xml in my code. I have currently got the file path hardcoded as shown below. I am trying to get the file path within my code without it being hardcoded.
I have tried using Path.GetDirectoryName and new FileInfo("Modules.xml").Directory.FullName. However, both of these target the file in my debug folder, when the file I need is in the main solution folder.
Is there a way to target the file in my main solution folder instead of my debug folder? (both files are called Modules.xml)
doc.Save("C:\\Users\\Matthew\\Desktop\\Year4\\Object Oriented\\Project1\\Project1\\Modules.xml");
Both file locations are shown below:
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\Modules.xml
^^^this is the file path I need for my code^^^
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\bin\Debug\Modules.xml
The best approach here would be to use a configuration file, e.g. app.config, for storing such a string. Then you can change file path without recompiling the code, and your file can be stored in any location accessible by application.
If you really want to access your file the way you explained, AppDomain.CurrentDomain.BaseDirectory will provide you with the bin/Debug location in runtime. Then you can find a relative path from there like:
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, #"..\..\", fileName);
where fileName is "Modules.xml" for example.
I have tried using Path.GetDirectoryName and new
FileInfo("Modules.xml").Directory.FullName. However, both of these
target the file in my debug folder, when the file I need is in the
main solution folder.
That's because bin\Debug is your working directory when you start and run the project. To change that, you can set the working directory environment variable to point to your solution directory (instead of bin\debug|release) which I wouldn't recommend that. Because when you finally endup with development, and release the application, there wouldn't be any solution directory that holds your XML file. What I can suggest is to copy your XML file to the output folder. Either you are in development (debug) or production (release) mode, the XML always going to be copied to final directory. And you can access the working directory with something like AppDomain.CurrentDomain.BaseDirectory. To enabling copy XML to output directory, right-click on it, choose Properties, set Build Action to None, and set Copy to Output Directory to Copy Always or Copy if newer. You're good to go now.
I want to show a local html file which exists in my project:
The calling file is the HelpFile.cs and in that the form contains the WebBrowser control.
The address I'm trying to reach is:
C:\Users\Keith\Desktop\Lingerie\Corset\Corset\Bra.html
The file is being called from:
The result is the dreaded page can't be displayed.
What I would like to do is be able to call the file as a relative html page. At a later stage, I would like to be able to call different help files.
Is this the best way to proceed or have I made a fundamental error and gone down the wrong path?
Any constructive help would be appreciated.
A file which exists in your project, lives in a specific location in your machine. But after you distribute the program and run it on user's machine, the file will not exists in the target machine.
You may want to distribute the file or add it as resource. To solve the problem you can use either of the following solutions:
You can copy the file to output directory at build time
You can add the file to a resource file like Resources.resx
You can make the file as an embedded resource
Then to show the file, you can use the following methods:
Get the file path and call the Navigate method or assign it to Url property
Get the resource content and assign it to DocumentText property
Get the resource stream and assign it to DocumentStream property
Copy the file to Output Directory
To copy the file to output directory at build time:
Solution explorer → See properties of your file
Set Build Action to Content.
Set Copy to Output Directory to Copy always.
Then the file will be copied to your output directory and you can use it this way:
var path = System.IO.Path.Combine(Application.StartupPath, "test.html");
this.webBrowser1.Navigate(path);
Please note, if the file is located under a folder in your project, for example under MyFolder, then it will be copied into a folder with the same name in the output directory of the application:
var path = System.IO.Path.Combine(Application.StartupPath, "MyFolder", "test.html");
this.webBrowser1.Navigate(path);
Add the file to a resx resource file like Resources.Resx
You can add the file to resource file of the project. This way it will be distributed in a resource assembly and you don't need to copy the file to output directory. To do so:
Solution explorer → Your project → Properties folder → open Resources.Resx file
From toolbar of the designer → Add existing file → Add the html file.
Then the content of the file will be available through a string property of the Resources. The property name will be same as the file name, for example if the file name is test.html, the property name will be test and You can use it this way:
this.webBrowser1.DocumentText = Properties.Resources.test;
Please note, for this solution the file doesn't need to be distributed by your project and it will be part of the resource file. However it will be part of your project file.
Make the file as an embedded resource
You can make the file as an embedded resource. This way it will be distributed in a resource assembly and you don't need to copy the file to output directory. To do so:
Solution explorer → See properties of your file
Set Build Action to Embedded Resource.
Set Copy to Output Directory to Do not copy.
Then to use, you need to get the file content from embedded resources. Assuming the file name is "test.html":
var fileName = "test.html";
var name = Assembly.GetExecutingAssembly().GetManifestResourceNames()
.Where(x => x.EndsWith(fileName)).First();
webBrowser1.DocumentStream =
Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
Please note, if you have the file inside a folder like MyFolder in the project, then the filename in above example will be "MyFolder.test.html".
XmlReader reader = XmlReader.Create(#"E:\NewFolder\WindowsFormsApplication1\WindowsFormsApplication1\QuestionFile.xml")
In my application i have read a xml file which is location in a specific location in my pc but now i want to deploy my application now when i rum my exe and install in other pc i get error that read error of xml file so what should i do for that.
like that i have used to read the xml file.
I would really appreciate if someone helps me out!
thanks
You could include the XML in the same folder as your program. In the code, build up the string dynamically, using the following to get the name of the folder the program is currently executing from:
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
All you need to do after that is append the name of your XML file with Path.Combine or appending to the string.
Edit:
(You'll need to include references to System.IO and System.Reflection).
You could create the string holding the path separately, then use that for creating your reader:
string xmlLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "QuestionFile.xml");
XmlReader reader = XmlReader.Create(xmlLocation);
Remember if you're running this in debug in VS, this will point to your debug directory so make sure a copy of the XML file is in there.
Can you package the questions.xml with the EXE as embedded resource?
Well, I think you have two options:
1) include the xml inside your solution as an embedded resource, and read it using GetManifestResourceStream. Here's more info on how to do it.
2) Include it in the solution and set the file's Build Action to Content. Then in your MSI installer package you can include that project's "Content" output. This means the file will end up included as a separate physical file located in the application's installation path.
See Here for more details. Step 2 shows how to add different outputs.
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")
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.