复制嵌入的资源作为文件保存到磁盘在C# [英] Copying embedded resource as file to disk in C#

查看:285
本文介绍了复制嵌入的资源作为文件保存到磁盘在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经保存在我的C#项目嵌入资源的INF文件。我想这个文件保存到按需本地位置。我使用这种方法。

I have an INF file saved as an embedded resource in my C# project. I am trying to save this file to a local location on demand. I am using this method.

public static void SaveResourceToDisk(string ResourceName, string FileToExtractTo)
{
    Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(ResourceName);
    FileStream resourceFile = new FileStream(FileToExtractTo, FileMode.Create);

    byte[] b = new byte[s.Length + 1];
    s.Read(b, 0, Convert.ToInt32(s.Length));
    resourceFile.Write(b, 0, Convert.ToInt32(b.Length - 1));
    resourceFile.Flush();
    resourceFile.Close();

    resourceFile = null;
}

当我尝试调用此方法(通过资源名称与命名空间一起名),我得到的错误:

When I try to call this method (passing the resource name along with the namespace name), I get the error:

对象引用未设置为一个对象

Object reference not set to an instance of an object

我在做什么错在这里?

推荐答案

您可以打电话

You could call

System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();

和视察该嵌入式访问这些资源。然后,你可以比较,对要传递什么,看看你确实完成你的预期是什么。

And inspect which embedded resources are accessible. Then you can compare that against what you are passing in to see if you are indeed accomplishing what you expected to.

此外,你应该考虑使用关键字来处置你的流:

Also, you should consider the using keyword to dispose of your streams:

using(FileStream ResourceFile = new FileStream(FileToExtractTo, FileMode.Create))
{
    //do stuff
}

祝你好运。

这篇关于复制嵌入的资源作为文件保存到磁盘在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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