Saturday, October 31, 2009

How to read text from file in C#

// Example #1

// Read the file as one string.
string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");

// Display the file contents to the console.
System.Console.WriteLine("Contents of writeText.txt = {0}", text);

// Example #2
// Read the file lines into a string array.
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");

No comments:

Post a Comment