Read a .txt resource embedded in a Class Library

  • Thread starter Thread starter Carlos Sosa Albert
  • Start date Start date
C

Carlos Sosa Albert

Hello guys,

Would anybody be so kind to tell me how to read a .txt I embedded in a C#
class library?

Thanks a lot,
 
So... I ended up with this code (now I'm looking how to convert a
StreamReader to a String using C#
But actually I don't remember it was so hard to obtain a string from a .txt
file embedded in a VB project. Am I wrong?

Assembly _assembly;
_assembly = Assembly.GetExecutingAssembly();
StreamReader _textStreamReader;
_textStreamReader = new
StreamReader(_assembly.GetManifestResourceStream("MyClassLibrary.MyFolder.MyFile.txt"));
 
So... I ended up with this code (now I'm looking how to convert a
StreamReader to a String using C#
But actually I don't remember it was so hard to obtain a string from a .txt
file embedded in a VB project. Am I wrong?

Assembly _assembly;
_assembly = Assembly.GetExecutingAssembly();
StreamReader _textStreamReader;
_textStreamReader = new
StreamReader(_assembly.GetManifestResourceStream("MyClassLibrary.MyFolder.MyFile.txt"));

By the time you've put the assignments in the same statements as the
declarations, that's two (admittedly long) lines of code. If that's
*really* too much, you can put it into a helper method really easily.

As for converting a StreamReader to a String - StreamReader.ReadToEnd
is your friend :)
 
Actually, I was sure I didn't have to specify "by hand" MyFile.txt but the
helper was doing so.
But maybe I wrote some code then to do that.
 
Oh, I remember... I used this... Wonder if it has something wrong...?
String xx = MyLibrary.Properties.MyResource;

Thanks Jon!
 
Back
Top