Possible Duplicate:
How to read embedded resource text file
我尝试创建一个简单的应用程序,该应用程序从“资源”文件中读取内容,我已经将其添加到我的解决方案中。我已经尝试过了,但这不起作用:
static void Main(string[] args)
{
StreamResourceInfo streamResourceInfo = Application.GetContentStream(new Uri("pack://application:,,,/myfile.txt", UriKind.Absolute));
StreamReader sr = new StreamReader(streamResourceInfo.Stream);
var content = sr.ReadToEnd();
Console.WriteLine(content);
}
它说:“无效的 URI:指定的端口无效。”
我该如何解决这个问题?
最佳答案
我的最终解决方案:
class Program
{
static void Main(string[] args)
{
Stream stream = typeof(Program).Assembly.GetManifestResourceStream("TheNameOfMyProject.TheNameOfSubFolder.file.txt");
StreamReader sr = new StreamReader(stream);
var content = sr.ReadToEnd();
Console.WriteLine(content);
Console.ReadLine();
}
}
关于c# - 从编译成 .NET 程序集的文本文件资源中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12856912/