Get File Bytes from Resource file in C#

99封情书 提交于 2020-01-03 15:34:17

问题


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.


回答1:


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);
            }
        }
    }
}



回答2:


Or add this in your resource class

internal static byte[] GetResource(string fileName) { 
    object obj = ResourceManager.GetObject(fileName, resourceCulture); 
    return ((byte[])(obj)); 
}


来源:https://stackoverflow.com/questions/1709373/get-file-bytes-from-resource-file-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!