1

I'd like to have a GUI that gives the user two options:

  1. Read in a key file loaded by the user through openFileDialog.
  2. Read in a preloaded key file (user would select one using radioButtons).

This would be helpful in that the user would not need to carry around all our key files, but it would still allow flexibility if a new key file needed to be added.

My code is currently working for option #1. I use:

readFile = new StreamReader(KeyFileFullPath);

where KeyFileFullPath is the filepath to the key file defined by the openFileDialog.

I would like to use the same streamReader for option #2, but I'm having trouble pointing the reader at the resource files.

From this question, I attempted the following:

_assembly = Assembly.GetExecutingAssembly();
readFile = new StreamReader(_assembly.GetManifestResourceStream(TM_Decoder.Properties.Resources._7p1_HOB_Key));

I navigated to ..."_7p1_HOB_Key" using C#'s autocomplete, so I'd expect it to be pointing me at something that actually exists. However, when I try to run the code, I get this error:

"Value cannot be null.Parameter name: stream"

Based on this, I tried looking up the ManifestResourceNames, but all it had was: "TM_Decoder.Form1.resources" and "TM_Decoder.Properties.Resources.resources"

Neither of these actually point at the key file I have loaded into my project's resources.

Thanks in advance for any help in getting the streamReader to point at the resource text file!

Edits (in response to SLaks suggestions):

  1. Unless I'm mistaken about the meaning of "Root Namespace," I think it is correct. The name of the project is "TM Decoder" so I think "TM_Decoder" is the root namespace. Is this not right?
  2. Thank you, I changed the build action to "Embedded Resource." It was previously set to "None" (I didn't know about it). Unfortunately, this wasn't enough to fix the issue (no change to the outcome of a debug attempt).
  3. I'm not sure about the items being in the Resource folder. They are in a folder called "resources" within the project folder, i.e. TM Decoder --> resources, as opposed to TM Decoder --> Properties --> Resources This is what Visual Studio did automatically when I added resources through the Resources tab in project properties.
Community
  • 1
  • 1
Patrigon
  • 157
  • 2
  • 9

1 Answers1

2

TM_Decoder.Properties.Resources._7p1_HOB_Key is a string containing the actual contents, not the resource name.
"TM_Decoder.Resources._7p1_HOB_Key" is the resource name.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • When I add quotes so that the line reads: ..._assembly.GetManifestResourceStream("TM_Decoder.Properties.Resources._7p1_HOB_Key"), it still gives a null value in readFile and returns the same error as before. – Patrigon Apr 09 '12 at 19:07
  • @Patrigon: You probably need to add the root namespace. – SLaks Apr 09 '12 at 19:35
  • Also, make sure that the file's build action is set to `Embedded Resource`. – SLaks Apr 09 '12 at 19:36
  • Also, if your file isn't in the `Resources\` folder, you'll need to change the path. – SLaks Apr 09 '12 at 19:36
  • @ SLaks: Thank you for your suggestions. Please take a look at my question edits if you are willing to give me a little more of your time. – Patrigon Apr 09 '12 at 20:23