18

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!

6 Answers 6

28

Assuming you are talking about files that are embedded as resources in your assembly:

var assembly = System.Reflection.Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream("SomeNamespace.somefile.png"))
{
    byte[] buffer = new byte[stream.Length];
    stream.Read(buffer, 0, buffer.Length);
    // TODO: use the buffer that was read
}
Sign up to request clarification or add additional context in comments.

1 Comment

ssuming you are talking about files that are embedded as resources in your assembly --> Correct assumption
22

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:

var myByteArray = Properties.Resources.MyFile;

3 Comments

Thank you, It did not work for my current application. However it did help out with some other programs I made before that use the resources stuff.
Where exactly can I change the FileType?
You can change the file extension from txt to bin before adding to Resources, then the type will be Binary automatically. Or just open Resources.resx with Text editor, and change System.String to System.Byte[].
1
byte[] capacity = Properties.Resources.excel;

enter image description here

Confusion: enter image description here

here we reading txt resource file .so it not return byte[] but it return string (file content).

Comments

0

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

Comments

0

Here's a little class we're using for this purpose:

static class EmbeddedResource
{
    /// <summary>
    /// Extracts an embedded file out of a given assembly.
    /// </summary>
    /// <param name="assemblyName">The namespace of your assembly.</param>
    /// <param name="fileName">The name of the file to extract.</param>
    /// <returns>A stream containing the file data.</returns>
    public static Stream Open(string assemblyName, string fileName)
    {
        var asm = Assembly.Load(assemblyName);
        var stream = asm.GetManifestResourceStream(assemblyName + "." + fileName);

        if (stream == null)
            throw new ConfigurationErrorsException(String.Format(
                    Strings.MissingResourceErrorFormat, fileName, assemblyName));

        return stream;
    }
}

Usage is pretty straightforward:

using (var stream = EmbeddedResource.Open("Assembly.Name", "ResourceName"))
    // do stuff

Comments

0
var rm = new ResourceManager("RessourceFile", typeof(ClassXY).Assembly);
return Encoding.UTF8.GetBytes(rm.GetString("key"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.