Adding and retrieving embedded resources codedom

一世执手 提交于 2019-12-07 04:23:07

问题


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

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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/13666956/adding-and-retrieving-embedded-resources-codedom

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