How do we read entire contents of a file named MsgSrikanth.txt(Text File Example) into a single string variable ?

 Posted by Chvrsri on 7/8/2011 | Category: .NET Framework Interview questions | Views: 3824 | Points: 40

There is a text file named "MsgSrikanth.txt" which has contents. Now we can convert that entire text file into a single string variable called showResult.

That conversion is shown below :

Dim showResult As String = NothingDim reader as New 

StreamReader("MsgSrikanth.txt")showResult = reader.ReadToEnd()

string showResult = NothingDim;

StreamReader reader = new StreamReader("MsgSrikanth.txt");
showResult = reader.ReadToEnd();

ReadToEnd() method used to read content from the current position to the end of the file.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Akiii on: 7/10/2011 | Points: 10
Can you please give the C# version of this ?

Thanks and Regards
Posted by: Chvrsri on: 7/11/2011 | Points: 10
Hi Akki,

I have updated this post with C# code. But i request you to not ask questions here.

Login to post response