How to read embedded resource text file

前端 未结 22 2323
悲&欢浪女
悲&欢浪女 2020-11-21 06:03

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

22条回答
  •  渐次进展
    2020-11-21 06:41

    You can use the Assembly.GetManifestResourceStream Method:

    1. Add the following usings

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

    3. 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.


    A no brainer astute to get the resourceName from the file name only (by pass the namespace stuff):

    string resourceName = assembly.GetManifestResourceNames()
      .Single(str => str.EndsWith("YourFileName.txt"));
    

    A complete example:

    public string ReadResource(string name)
    {
        // Determine path
        var assembly = Assembly.GetExecutingAssembly();
        string resourcePath = name;
        // Format: "{Namespace}.{Folder}.{filename}.{Extension}"
        if (!name.StartsWith(nameof(SignificantDrawerCompiler)))
        {
            resourcePath = assembly.GetManifestResourceNames()
                .Single(str => str.EndsWith(name));
        }
    
        using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
        using (StreamReader reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
        }
    }
    

提交回复
热议问题