Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get File Bytes from Resource file in C#

Tags:

c#

resources

byte

I used to store the resources in the actual project, but I've switched to using a resource file instead. Originally I could ready the bytes for a file, but I'm finding it difficult doing this with a resource file. Any suggestions would be greatly appreciated.

like image 432
williamtroup Avatar asked Dec 09 '22 18:12

williamtroup


1 Answers

public static byte[] ReadResource(string resourceName)
{
    using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
    {
        byte[] buffer = new byte[1024];
        using (MemoryStream ms = new MemoryStream())
        {
            while (true)
            {
                int read = s.Read(buffer, 0, buffer.Length);
                if (read <= 0)
                    return ms.ToArray();
                ms.Write(buffer, 0, read);
            }
        }
    }
}
like image 148
Anton Avatar answered Dec 25 '22 11:12

Anton