Patrigon
Patrigon

Reputation: 157

Trouble getting contents of resource text file into StreamReader

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.

Upvotes: 1

Views: 1009

Answers (1)

SLaks
SLaks

Reputation: 887285

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.

Upvotes: 2

Related Questions