How to read embedded resource text file

前端 未结 22 2366
悲&欢浪女
悲&欢浪女 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:52
    public class AssemblyTextFileReader
    {
        private readonly Assembly _assembly;
    
        public AssemblyTextFileReader(Assembly assembly)
        {
            _assembly = assembly ?? throw new ArgumentNullException(nameof(assembly));
        }
    
        public async Task<string> ReadFileAsync(string fileName)
        {
            var resourceName = _assembly.GetManifestResourceName(fileName);
    
            using (var stream = _assembly.GetManifestResourceStream(resourceName))
            {
                using (var reader = new StreamReader(stream))
                {
                    return await reader.ReadToEndAsync();
                }
            }
        }
    }
    
    public static class AssemblyExtensions
    {
        public static string GetManifestResourceName(this Assembly assembly, string fileName)
        {
            string name = assembly.GetManifestResourceNames().SingleOrDefault(n => n.EndsWith(fileName, StringComparison.InvariantCultureIgnoreCase));
    
            if (string.IsNullOrEmpty(name))
            {
                throw new FileNotFoundException($"Embedded file '{fileName}' could not be found in assembly '{assembly.FullName}'.", fileName);
            }
    
            return name;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 06:53

    When you added the file to the resources, you should select its Access Modifiers as public than you can make something like following.

    byte[] clistAsByteArray = Properties.Resources.CLIST01;
    

    CLIST01 is the name of the embedded file.

    Actually you can go to the resources.Designer.cs and see what is the name of the getter.

    0 讨论(0)
  • 2020-11-21 06:56

    Something I learned just now is that your file is not allowed to have a "." (dot) in the filename.

    A "." in filename is no good.

    Templates.plainEmailBodyTemplate-en.txt --> Works!!!
    Templates.plainEmailBodyTemplate.en.txt --> doesn't work via GetManifestResourceStream()

    Probably because the framework gets confused over namespaces vs filename...

    0 讨论(0)
  • 2020-11-21 06:56

    For users that are using VB.Net

    Imports System.IO
    Imports System.Reflection
    
    Dim reader As StreamReader
    Dim ass As Assembly = Assembly.GetExecutingAssembly()
    Dim sFileName = "MyApplicationName.JavaScript.js" 
    Dim reader = New StreamReader(ass.GetManifestResourceStream(sFileName))
    Dim sScriptText = reader.ReadToEnd()
    reader.Close()
    

    where MyApplicationName is namespace of my application. It is not the assembly name. This name is define in project's properties (Application tab).

    If you don't find correct resource name, you can use GetManifestResourceNames() function

    Dim resourceName() As String = ass.GetManifestResourceNames()
    

    or

    Dim sName As String 
        = ass.GetManifestResourceNames()
            .Single(Function(x) x.EndsWith("JavaScript.js"))
    

    or

    Dim sNameList 
        = ass.GetManifestResourceNames()
            .Where(Function(x As String) x.EndsWith(".js"))
    
    0 讨论(0)
  • 2020-11-21 06:57

    I know it is an old thread, but this is what worked for me :

    1. add the text file to the project resources
    2. set the access modifier to public, as showed above by Andrew Hill
    3. read the text like this :

      textBox1 = new TextBox();
      textBox1.Text = Properties.Resources.SomeText;
      

    The text that I added to the resources: 'SomeText.txt'

    0 讨论(0)
  • 2020-11-21 06:57

    For all the people that just quickly want the text of a hardcoded file in winforms;

    1. Right-click your application in the solution explorer > Resources > Add your file.
    2. Click on it, and in the properties tab set the "FileType" to "Text".
    3. In your program just do Resources.<name of resource>.toString(); to read the file.

    I would not recommend this as best practice or anything, but it works quickly and does what it needs to do.

    0 讨论(0)
提交回复
热议问题