Create file info from resources c#.NET - c#

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.

Related

Accessing location of embedded XML file

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.

Read File properties

I am trying to read the file properties, For example If i change the file extension of test.txt file into test_txt.vsf, the type of file still .txt in file properties. I want to read this file extension from properties.
I am usinf below code which displays the file name extension as .vsf. But actually it's extension is .txt.
FileInfo info = new FileInfo(#"C:\Users\saravana_rajkumar\Desktop\Test_txt.vsf");
Console.WriteLine(info.Extension);
Please guide...
The type of the data actually in a file is not stored anywhere by Windows. It is up to applications to determine if they can handle a file they are given.
For example, if you rename an EXE to ".txt" you can try to open it with Notepad, and it will try to open it as a text file.
When you say this:
If i change the file extension of test.txt file into test_txt.vsf, the type of file still .txt in file properties.
You are wrong. The type of the file is not still ".txt" in file properties. The file properties for file type in Windows Explorer works solely off the file suffix.
Do you tried to use Path.ChangeExtension method ?
Check out Path.ChangeExtension documentation in MSDN
Example:
string newFileName = Path.ChangeExtension("test_txt.txt", ".vsf");

Embedding a word file as a resource

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 .

Extract and open PPT from resources in C#

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.

Open HTML file whose relative path is known in a browser using C#

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

Categories

Resources