Saturday, 16 June 2012

.Net Beginners , C# Tip : Read the whole file and return a string of data using C# and VB.Net

There is a very quick way in .Net to read the entire file and return a string of data.
We are using ReadAllText method of File class.
Here are sample example of this.
In this example we read the whole file in one statement and return it's string of data.
These examples are in both C# and VB.Net .

C# Example :
 // Read the entire file and returns a string of data
 string strData = System.IO.File.ReadAllText(MapPath("TextFile.txt"));

VB.net Example :
'' Read the entire file and returns a string of data
Dim strData As String = System.IO.File.ReadAllText(MapPath("TextFile.txt"))

This is very useful .Net Tips which is use in day to day programming life.


No comments:

Post a Comment