Posted By

stewshack on 05/16/09


Tagged

file io c streamReader


Versions (?)

Who likes this?

9 people have marked this snippet as a favorite

rtipton
SnipItPrpDemo
LeeRJohnson
umang_nine
vali29
rageit
khouser
Priestd09
danfinrud55


C# Reading a File


 / Published in: C#
 

This is how I read a file using C#.

  1. string DeathStarPlans = @"C:\DeathStarPlans.txt";
  2.  
  3. StringBuilder R2D2 = new StringBuilder();
  4.  
  5. using ( StreamReader PrincessLeia = new StreamReader( DeathStarPlans ) )
  6. {
  7. while ( PrincessLeia.Peek() >= 0 )
  8. {
  9. R2D2.Append( PrincessLeia.ReadLine() );
  10. }
  11. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: freshclickmedia on June 17, 2010

// my version of reading a file:

using ( StreamReader PrincessLeia = new StreamReader( DeathStarPlans ) ) { string R2D2 = PrincessLeia.ReadToEnd(); }

Posted By: stewshack on June 24, 2010

That works too, but I like to Peek just in case I need to do something for each line. Thanks for the feedback.

You need to login to post a comment.