如何使用StreamReader读取嵌入式资源(文本文件)并将其作为字符串返回?我当前的脚本使用Windows窗体和文本框,允许用户查找和替换未嵌入的文本文件中的文本。

private void button1_Click(object sender, EventArgs e)
{
    StringCollection strValuesToSearch = new StringCollection();
    strValuesToSearch.Add("Apple");
    string stringToReplace;
    stringToReplace = textBox1.Text;

    StreamReader FileReader = new StreamReader(@"C:\MyFile.txt");
    string FileContents;
    FileContents = FileReader.ReadToEnd();
    FileReader.Close();
    foreach (string s in strValuesToSearch)
    {
        if (FileContents.Contains(s))
            FileContents = FileContents.Replace(s, stringToReplace);
    }
    StreamWriter FileWriter = new StreamWriter(@"MyFile.txt");
    FileWriter.Write(FileContents);
    FileWriter.Close();
}

当前回答

您也可以使用@dtb答案的简化版本:

public string GetEmbeddedResource(string ns, string res)
{
    using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format("{0}.{1}", ns, res))))
    {
        return reader.ReadToEnd();
    }
}

其他回答

基本上,您使用System.Reflection获取对当前程序集的引用。然后,使用GetManifestResourceStream()。

例如,从我发布的页面:

注:需要添加使用System.Reflection;让它发挥作用

   Assembly _assembly;
   StreamReader _textStreamReader;

   try
   {
      _assembly = Assembly.GetExecutingAssembly();
      _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyTextFile.txt"));
   }
   catch
   {
      MessageBox.Show("Error accessing resources!");
   }

当您将文件添加到资源中时,您应该将其“访问修改器”选择为“公共”,然后再进行如下操作。

byte[] clistAsByteArray = Properties.Resources.CLIST01;

CLIST01是嵌入文件的名称。

实际上,您可以访问resources.Designer.cs,查看getter的名称。

我刚才了解到,文件名中不允许有“.”(点)。

Templates.plainMailBodyTemplate-en.txt-->工作!!!Templates.plainMailBodyTemplate.en.txt-->无法通过GetManifestResourceStream()工作

可能是因为框架混淆了名称空间和文件名。。。

某些VS.NET项目类型不会自动生成.NET(.resx)文件。以下步骤将资源文件添加到项目中:

右键单击项目节点并选择Add/NewItem,滚动到Resources File。在“名称”框中选择适当的名称,例如“资源”,然后单击“添加”按钮。资源文件Resources.resx被添加到项目中,并且可以作为解决方案资源管理器中的节点。实际上,创建了两个文件,还有一个自动生成的C#类Resources.Designer.cs。不要编辑它,它由VS维护。该文件包含一个名为Resources的类。

现在,您可以添加文本文件作为资源,例如xml文件:

双击Resources.resx。选择“添加资源”>“添加现有文件”,然后滚动到要包含的文件。将默认值“内部”保留为“访问修改”。图标表示新资源项。如果选中,属性窗格将显示其财产。对于xml文件,在属性Encoding下选择Unicode(UTF-8)–Codepage 65001,而不是默认的本地代码页。对于其他文本文件,请选择此文件的正确编码,例如代码页1252。对于像xml文件这样的文本文件,类Resources具有一个字符串类型的属性,该属性以包含的文件命名。如果文件名为例如RibbonManifest.xml,则属性的名称应为RibbonManifest。您可以在代码文件Resources.Designer.cs中找到确切的名称。像使用任何其他字符串属性一样使用字符串属性,例如:string xml=Resources.RibbonManifest。一般形式为ResourceFileName.IncludedTextFileName。不要使用ResourceManager.GetString,因为字符串属性的get函数已经完成了这一操作。

可以使用两种不同的方法将文件添加为资源。

访问文件所需的C#代码不同,这取决于首先添加文件所用的方法。

方法1:添加现有文件,将属性设置为Embedded Resource

将文件添加到项目中,然后将类型设置为“嵌入式资源”。

注意:如果使用此方法添加文件,则可以使用GetManifestResourceStream访问它(请参阅@dtb的答案)。

方法2:将文件添加到Resources.resx

打开Resources.resx文件,使用下拉框添加文件,将AccessModifier设置为public。

注意:如果使用此方法添加文件,则可以使用财产.资源访问它(请参阅@Night Walker的回答)。