Using Resource Files in App

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.

I want something like:

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

Obviously, this will not compile.

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

Chuck D

If you have an embedded resource added to your project in the way you
described, you can access it through My.Resources.

For example, if the resource is called ThePDF you can access it as:

My.Resources.ThePDF

Files like PDFs get encoded in the resource as a byte array. The below bit
of code would extract the resource and save it to a real file (probably not
what you want).

Dim fs as As System.IO.FileStream = New System.IO.FileStream("ThePDF.pdf",
System.IO.FileMode.Create)
fs.Write(My.Resources.ThePDF, 0, My.Resources.ThePDF.Length)
fs.Close()


Chuck
 
J

jp2msft

Thanks! That was what I needed.

Chuck D said:
If you have an embedded resource added to your project in the way you
described, you can access it through My.Resources.

For example, if the resource is called ThePDF you can access it as:

My.Resources.ThePDF

Files like PDFs get encoded in the resource as a byte array. The below bit
of code would extract the resource and save it to a real file (probably not
what you want).

Dim fs as As System.IO.FileStream = New System.IO.FileStream("ThePDF.pdf",
System.IO.FileMode.Create)
fs.Write(My.Resources.ThePDF, 0, My.Resources.ThePDF.Length)
fs.Close()


Chuck
 

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