【发布时间】:2013-09-19 14:48:34
【问题描述】:
我正在尝试从 Visual Studio 中的资源中访问和读取文本文件“achInfo.txt”。这个网站上已经列出了一些解决方法,但它们似乎都不适合我。他们只是给了我两个错误之一,我稍后会解释。
这是到目前为止的整个方法。
private string[] GetAchMetadata(short element)
{
string[] temp = { "", "" };
string cLine;
try
{
StreamReader sr = new StreamReader(Properties.Resources.achInfo);
while (!sr.EndOfStream)
{
cLine = sr.ReadLine();
if (Microsoft.VisualBasic.Information.IsNumeric(cLine))
{
if (int.Parse(cLine) == element)
{
temp[0] = cLine;
temp[1] = sr.ReadLine();
return temp;
}
}
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("There was a problem in collecting data.");
System.Diagnostics.Debug.Write(e);
}
return temp;
}
我的第一个假设是使用Properties.Resources.achInfo,因为它直接指向相关文件。但是,这会引发 System.ArgumentException 错误,描述为“路径中的非法字符”。
然后我使用了组装解决方案('Grandma_Lair' 是我的命名空间,不要问。'):
Assembly asm;
asm = Assembly.GetExecutingAssembly();
StreamReader sr = new StreamReader(asm.GetManifestResourceStream("Grandma_Lair.achInfo.txt"));
但是,这会引发System.ArgumentNullException,并显示消息“值不能为空”。
我还将我的资源上的访问修饰符设置为公共,并确保文件设置为嵌入式资源。
有人知道我的问题吗?
【问题讨论】:
-
您说文件设置为嵌入式资源,但复制操作是否设置为“从不复制”以外的任何内容?
标签: c# file visual-studio-2012 stream