添加和检索嵌入的资源的CodeDOM [英] Adding and retrieving embedded resources codedom

查看:167
本文介绍了添加和检索嵌入的资源的CodeDOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我觉得像回答我的问题是网上的,但我不能找到它。所有我想要做的就是添加一个文本资源文件我用的CodeDOM编译程序,然后访问该文本文件在编制程序。要添加嵌入的资源,我用下面的代码:

Ok, I feel like the answer to my question is online, but I cannot find it. All I'm trying to do is add a text resource file to the program I'm compiling with CodeDom and then access that text file in the compiled program. To add the embedded resource, I used the following code:

        System.Resources.ResourceWriter writer = new System.Resources.ResourceWriter("Resources.resx");
        writer.AddResource("EoS.txt", Form1.MasterEncoder.GetBytes(Properties.Resources.Eos));
        writer.Generate();
        writer.Close();
        Parameters.EmbeddedResources.Add("Resources.resx");

下面的参数是我的编译器参数对象和Eos.txt是我的文件试图写,这是在应用程序的资源。
这时候我建立程序没有抛出任何错误,但是当我试图抓住使用此代码,这是我加入到窗体加载事件在新编译的程序这个文本文件:

Here "Parameters" is my compiler parameters object and "Eos.txt" is the file I'm trying to write, which is a resource in the application. This doesn't throw any errors when I build the program, but when I try to grab this text file in the newly compiled program using this code, which I added to the form load event:

         _assembly = Assembly.GetExecutingAssembly();
         _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("Resources.EoS.txt"));
         TextFile = _textStreamReader.ReadToEnd();



我得到这个错误大致为:值不能为空参数名:流。我已经试过资源转变为程序和许多其他小的调整的空间,并没有奏效。我不知道如果我完全错误的轨道与否,但我想我的问题是与CodeDom中。有什么建议?

I get this error roughly: "Value cannot be null Parameter name: stream." I've tried changing "Resources" to the namespace of the program and many other little tweaks and nothing has worked. I'm not sure if I'm on the completely wrong track or not, but I think my issue is with CodeDom. Any suggestions?

推荐答案

这是几乎可以肯定,由于形成资源名称意想不到的方式。一招时,我不记得了我的头顶,我用的是临时添加此行代码:

It's almost certainly due to unexpected way the resource names are formed. One trick I use when I can't remember off the top of my head, is to temporarily add this line of code:

string[] resourceNames = _assembly.GetManifestResourceNames();



设置上有一个断点,从而提醒正确的名称的自己,然后复制到代码

Set a breakpoint on it, and thus remind myself of the correct name, then copy that into the code.

这篇关于添加和检索嵌入的资源的CodeDOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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