繁体   English   中英

从C#中的资源文件中获取文件字节

[英]Get File Bytes from Resource file in C#

我曾经将资源存储在实际项目中,但我转而使用资源文件。 最初我可以准备一个文件的字节,但我发现很难用资源文件做这个。 任何建议将不胜感激。

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

或者在资源类中添加它

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM