Loading a File from a Resource

J

jp2msft

How would I load a file from a resource?

We have a 'PDF-like' file that is formatted for printing the our label forms
(it isn't quite like a PDF, but pretty close. It lets us enter text that
comes out as a barcode).

Anyway, the file often gets lost or deleted by someone fooling around with
the PC when they should not be.

To resolve this problem, I inserted the file under Project Properties >
Resources > Files section.

To load the file, I tried:

File.Open(global::project2.Properties.Resources.File1)

But that would not compile.

Can I use the Resource object to store files that I can access in my
application? If so, how?
 
A

Alberto Poblacion

jp2msft said:
Can I use the Resource object to store files that I can access in my
application? If so, how?

One way to embed and use a file as a resource (which I don't defend as ideal
or optimal) is the following:

- Add the file to your project (possibly under a subfolder) and in the
properties of the file, under "compile action", select "embedded resource".

- When you want to use the file from your code, do the following:

System.Reflection.Assembly assm =
System.Reflection.Assembly.GetExecutingAssembly();
using (System.IO.Stream stm =
assm.GetManifestResourceStream("theNamespace.subfolder.theFile.pdf"))
{
//Here stm is a Stream opened on your file
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top