2

可能重复:
如何读取嵌入的资源文本文件

我尝试创建一个从“资源”文件中读取的简单应用程序,该文件已添加到我的解决方案中。我试过这个,但这不起作用:

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:指定的端口无效。”

我该如何解决这个问题?

4

1 回答 1

4

我的最终解决方案:

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();
    }
}
于 2012-10-12T12:51:49.783 回答