Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2007

Tip: Looking for answers? Try searching our database.

Read a .txt resource embedded in a Class Library

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carlos Sosa Albert - 07 Jun 2007 21:27 GMT
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,
Carlos Sosa Albert - 07 Jun 2007 21:53 GMT
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"));

> 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,
Jon Skeet [C# MVP] - 07 Jun 2007 22:39 GMT
> So... I ended up with this code (now I'm looking how to convert a
> StreamReader to a String using C#
[quoted text clipped - 6 lines]
>     _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 :)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Carlos Sosa Albert - 08 Jun 2007 14:47 GMT
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.

>> So... I ended up with this code (now I'm looking how to convert a
>> StreamReader to a String using C#
[quoted text clipped - 14 lines]
> As for converting a StreamReader to a String - StreamReader.ReadToEnd
> is your friend :)
Carlos Sosa Albert - 08 Jun 2007 14:59 GMT
Oh, I remember... I used this... Wonder if it has something wrong...?
String xx = MyLibrary.Properties.MyResource;

Thanks Jon!

>> So... I ended up with this code (now I'm looking how to convert a
>> StreamReader to a String using C#
[quoted text clipped - 14 lines]
> As for converting a StreamReader to a String - StreamReader.ReadToEnd
> is your friend :)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.