无法将资源文本文件的内容导入StreamReader [英] Trouble getting contents of resource text file into StreamReader
问题描述
我想拥有一个为用户提供两个选项的GUI:
I'd like to have a GUI that gives the user two options:
- 通过以下方式读取用户加载的密钥文件openFileDialog。
- 读入预加载的密钥文件(用户可以使用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.
我的代码当前适用于选项#1。我使用:
My code is currently working for option #1. I use:
readFile = new StreamReader(KeyFileFullPath);
其中,KeyFileFullPath是openFileDialog定义的密钥文件的文件路径。
where KeyFileFullPath is the filepath to the key file defined by the openFileDialog.
我想对选项#2使用相同的streamReader,但是我无法将阅读器指向资源文件。
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));
我使用C#的自动完成功能导航到... _ 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:
值不能为空。参数名称:stream
"Value cannot be null.Parameter name: stream"
基于 ,我尝试查找ManifestResourceNames,但实际上只有:
TM_Decoder.Form1.resources和 TM_Decoder.Properties.Resources.resources
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.
在此先感谢您对streamReader的帮助。指向资源文本文件!
Thanks in advance for any help in getting the streamReader to point at the resource text file!
编辑(以响应SLaks的建议):
Edits (in response to SLaks suggestions):
- 除非我对根命名空间的含义有误,否则我认为它是正确的。该项目的名称是 TM Decoder,所以我认为 TM_Decoder是根名称空间。
- 谢谢,我将构建操作更改为嵌入式资源。以前它设置为无(我不知道)。不幸的是,这还不足以解决问题(调试尝试的结果没有变化)。
- 我不确定资源文件夹中的项目。它们位于项目文件夹中名为 resources的文件夹中,即
TM Decoder->资源,而不是TM Decoder-> Properties-> Resources
这是Visual Studio自动执行的操作当我通过项目属性中的资源选项卡添加资源时。
推荐答案
TM_Decoder.Properties.Resources._7p1_HOB_Key
是一个包含实际内容而不是资源名称的字符串。
TM_Decoder.Resources._7p1_HOB_Key
是资源名称。
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.
这篇关于无法将资源文本文件的内容导入StreamReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!