How to read embedded resource text file

How do I read an embedded resource (text file) using StreamReader and return it as a string? My current script uses a Windows form and textbox that allows the user to find and replace text in a text file that is not embedded.

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

You can use the Assembly.GetManifestResourceStream Method:

  • Add the following usings

    using System.IO;
    using System.Reflection;
    
  • Set property of relevant file:
    Parameter Build Action with value Embedded Resource

  • Use the following code

  • var assembly = Assembly.GetExecutingAssembly();
    var resourceName = "MyCompany.MyProduct.MyFile.txt";
    
    using (Stream stream = assembly.GetManifestResourceStream(resourceName))
    using (StreamReader reader = new StreamReader(stream))
    {
        string result = reader.ReadToEnd();
    }
    

    resourceName is the name of one of the resources embedded in assembly . For example, if you embed a text file named "MyFile.txt" that is placed in the root of a project with default namespace "MyCompany.MyProduct" , then resourceName is "MyCompany.MyProduct.MyFile.txt" . You can get a list of all resources in an assembly using the Assembly.GetManifestResourceNames Method.


    You can add a file as a resource using two separate methods.

    The C# code required to access the file is different, depending on the method used to add the file in the first place.

    Method 1: Add existing file, set property to Embedded Resource

    Add the file to your project, then set the type to Embedded Resource .

    NOTE: If you add the file using this method, you can use GetManifestResourceStream to access it (see answer from @dtb).

    Method 2: Add file to Resources.resx

    Open up the Resources.resx file, use the dropdown box to add the file, set Access Modifier to public .

    NOTE: If you add the file using this method, you can use Properties.Resources to access it (see answer from @Night Walker).

    在这里输入图像描述


    Take a look at this page: http://support.microsoft.com/kb/319292

    Basically, you use System.Reflection to get a reference to the current Assembly. Then, you use GetManifestResourceStream() .

    Example, from the page I posted:

    Note : need to add using System.Reflection; for this to work

       Assembly _assembly;
       StreamReader _textStreamReader;
    
       try
       {
          _assembly = Assembly.GetExecutingAssembly();
          _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyTextFile.txt"));
       }
       catch
       {
          MessageBox.Show("Error accessing resources!");
       }
    
    链接地址: http://www.djcxy.com/p/23712.html

    上一篇: Android:我如何从使用名字的资源获取字符串?

    下一篇: 如何阅读嵌入式资源文本文件