我在资源文件夹中的winform C#项目中添加了一个文本文件资源 这里是我添加
后的Resources.resx的内容<data name="_0" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\0.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
然后我尝试在
之前使用此解决方案阅读嵌入式资源的内容How to read embedded resource text file
这是主程序
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "Properties.Resources.0.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
try
{
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
Console.WriteLine(result);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
但是我在var阅读器中得到了null参数,我认为它是关于资源名称的路径,任何IDEA来解决我的问题?
之前谢谢答案 0 :(得分:0)
您必须将您的项目命名空间附加到resourcename:
var resourceName = "Properties.Resources.0.txt";
将其更改为:
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "ProjectNamespace.Properties.Resources.0.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
}