I have added an XML file to a Windows form project by right clicking on resources | add item and I have set the properties to embedded resource, do not copy. I need to copy the XML file to a location on the target disk but how do I get the location of the XML file from the embedded resources to use with File.Copy
Thanks.
Use System.Reflection.Assembly.GetManifestResourceNames. It will return a list of all of the resources within the assembly, and you can use it (in the Visual Studio immediate window) to determine what string you need to pass to get the resource you want.
From there, you need to use System.Reflection.Assembly.GetManifestResourceStream. To get a stream of the file. Then you can use the CopyTo method to copy it to a FileStream of a new file of your choosing.
Related
I have a .NET executable and I need to view the resources attached to it. I extracted .resource file from .NET executable using DotNetResourcesExtract utility but I don't now how to view content of .resource file.
Could someone explain how to view this file?
Not sure you're using it correctly...
Assuming your storing images there.
You can simply do:
Image resfile = ProjectName.Properties.Resources.resourceName;
So if resfile is an image, you can put it into an Image control.
so, if you have an image control on your form you can simply do:
imageControl.Image = ProjectName.Properties.Resources.resourceName;
If it's a text file or any other type of file - again, you can access it the same way. If it's a binary file, the ProjectName.Properties.Resources.resourceName will be a byte array, so you 'll need to load it in the correct manner.
Is that what you're wanting? Otherwise indicate what type of file are you trying to extract from your resource file.
According to MSDN The .resx (XML-based resource format) files are
converted into common language runtime binary .resources files
that can be embedded in a runtime binary executable or compiled into
satellite assemblies.
Getting to the point: How to view this?
Well, Since it's a binary file which contains resource(images etc.) therefore you could always use Windows resources editing/extracting applications.
eg. Restorator, Resource Hacker to name a few.
Meanwhile, have look at this Stackoverflow post. which sound almost similar.
I have in the resources of my visual studio C# projetc a xlsx file and I want to manipulate it by FastExcel library (https://github.com/mrjono1/FastExcel), but, like see you in the github's page code, I have to create a FileInfo object and it has only one constructor that wants a file's path but only link that I have of the file is the stream that I get by this line of code:
test.Properties.Resources.test1
How do I pass throught from link to resources to path for create a FileInfo?
Well you can write the Stream to disk and provide the file path to the FileInfo constructor. I would create a seperate utility that can accepts a Stream and returns a file path.
I am trying to access a word file from my c# code. I want to embed it as part of a project so that when i call a word doc object to be read from an aspx page, the object should have access to that word doc no matter where it is being used from. So how can i include a word doc as a resource in my project? Also in order to open my word doc, i need a path for that doc. After i have included the file as a resource, how do i somehow get its "path" so the word doc object can be created? Is there someway to copy it over to a temp location on whatever machine is calling that object?
Thanks
Note: Do not use MSWord in server environment.
How to embed file as resource
After getting the stream use Stream.CopyTo to save it to FileStream created on temporary file Path.GetTempFileName or in temp folder .
I want to view presentation in PowerPoint viewer, ppt file is in a resources. so the problem is that how can i access it and view in PowerPoint viewer.
Here is sample code
Process.Start(#"C:\Program Files\Microsoft Office\Office12\PPTVIEW.exe",**#"e:\presentation.ppt")**;
How can i replace this path by ppt containing in resources?
Actually, what you ask for is a common pattern and there are some related questions and answers here on SO.
Basically what you do in general is the following:
locate the resource in question and open a resource stream to it.
Save the stream to a (temporary) file if your target API cannot deal with streams or byte arrays directly.
Perform whatever operation on the file or directly on the stream/byte array (as I said, if supported).
Eventually remove the temporary file, if any, from step 1.
So, you first extract the PPT file (actually it doesn't really matter that it is a PPT file, could by any file or byte blob for that matter).
string tempFile = Path.GetTempFileName();
using (Stream input = assembly.GetManifestResourceStream("MyPresentation.PPT"))
using (Stream output = File.Create(tempFile))
{
input.CopyTo(output); // Stream.CopyTo() is new in .NET 4.0, used for simplicity and illustration purposes.
}
Then you open it using Process.Start(). You don't need to specify the path to the Powerpoint executable, as PPT should be a registered file extension with either PowerPoint or the PowerPoint Viewer. If you have both installed, you may still want to provide the path to the relevant executable to prevent launching the wrong application. Make sure that you don't hardcode the path though, but try to retrieve it from the registry (or similar, I haven't checked because that gets too specific now).
using (var process = Process.Start(tempFile))
{
process.WaitForExit();
// remove temporary file after use
File.Delete(tempFile);
}
Note: I left out quite some error handling that you might want to add in a real application.
On running my console-based C# application it produces a Results.htm file at a location "../../Template/Output/" and at the end of the program I want to open the Results.htm file in a browser.
Currently, Process.Start("http://www.google.com"); is working but, how do I open a file whose file name and relative path are known?
Should I somehow get the full path of the Results.htm file and use that?
If yes, How do I do that?
Any pointers would help.
you can use the FileInfo with the constructor of the relative file
and then see its attribute of FullName