How to read embedded resource text file

前端 未结 22 2401
悲&欢浪女
悲&欢浪女 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:39

    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!");
       }
    

提交回复
热议问题