Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding and retrieving embedded resources 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");

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?

like image 953
user1869878 Avatar asked Nov 12 '22 16:11

user1869878


1 Answers

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.

like image 119
Chris Ballard Avatar answered Nov 15 '22 05:11

Chris Ballard