【问题标题】:Reading from a text file resource which is compiled into a .NET assembly [duplicate]从编译成.NET程序集的文本文件资源中读取[重复]
【发布时间】:2012-10-03 03:24:19
【问题描述】:

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

我该如何解决这个问题?

【问题讨论】:

标签: c# .net resources


【解决方案1】:

我的最终解决方案:

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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 2019-08-19
    • 2018-12-28
    • 1970-01-01
    相关资源
    最近更新 更多