I've been trying to figure out how to read a byte array from one of my resource files, I have tried the most popular hits on Google without clear success.
I have a file stored in the resource collection of my program, I'd like to read this file as a byte array
I'm currently just reading the file from the root directory of my program with the following code:
FileStream fs = new FileStream(Path, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();
However I want to store this file as a resource in my application, so that I don't have to ship an extra file with my program.
This file holds encrypted data that part of my program uses.
Any help or pointers would be greatly appreciated!
You can add resources to your application by going in the properties of the project, "Resources" tab (create one if needed), Add Resource (existing file). When your file is added, you can set its FileType (in its properties) to Binary.
Documentation
After that, you can access to your file as a byte[] easily:
Here's a little class we're using for this purpose:
Usage is pretty straightforward:
maybe you can try using StreamResourceInfo. Here is a link to a Silverlight example, but if I am not mistaken you should be able to apply the same principles in any .NET application:
http://msdn.microsoft.com/en-us/library/system.windows.resources.streamresourceinfo(v=VS.95).aspx
Regards,
Anders @ Cureos
Assuming you are talking about files that are embedded as resources in your assembly: