c# - 从编译成 .NET 程序集的文本文件资源中读取

标签 c# .net resources

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/

相关文章:

wpf - 如何访问属于资源一部分的 ContextMenu 中的 menuitem

resources - 有关 Lazarus 和/或 FPC 的活跃博客

c# - 如何在 Enterprise Architect 中为包提供构造型?

c# - 如何将自定义类保存/序列化到设置文件?

C# Method Attribute强制要求是abstract还是static?

c# Linq 关键字?

c# - Silverlight 验证。电子邮件验证问题

c# - 具有多种类型的列表

c# - Process.Kill() 访问被拒绝

c# - 在小型 .NET 程序中管理临时文件