从获取的资源文件在C#文件字节 [英] Get File Bytes from Resource file in C#

查看:196
本文介绍了从获取的资源文件在C#文件字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前的资源存储在实际的项目,但我已经切换到使用资源文件来代替。本来我可以准备一个文件的字节,但是我发现有一个资源文件,很难这样做。任何建议,将不胜感激。


解决方案

 公共静态的byte [] ReadResource(字符串资源名称。)使用
{
(流S = Assembly.GetExecutingAssembly()GetManifestResourceStream(资源名称))
{
字节[]缓冲区=新的字节[1024];使用
(MemoryStream的毫秒=新的MemoryStream())
{
,而(真)
{
INT读= s.Read(缓冲,0,buffer.Length );
如果(读< = 0)
返回ms.ToArray();
ms.Write(缓冲,0,读);
}
}
}
}


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.

解决方案

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

这篇关于从获取的资源文件在C#文件字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆