一次读一行:
int counter = 0;
string line;// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}file.Close();// Suspend the screen.
Console.ReadLine();从文本文件中读:
// Read the file as one string.
System.IO.StreamReader myFile =
new System.IO.StreamReader("c:\\test.txt");
string myString = myFile.ReadToEnd();myFile.Close();// Display the file contents.
Console.WriteLine(myString);
// Suspend the screen.
Console.ReadLine();
int counter = 0;
string line;// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}file.Close();// Suspend the screen.
Console.ReadLine();从文本文件中读:
// Read the file as one string.
System.IO.StreamReader myFile =
new System.IO.StreamReader("c:\\test.txt");
string myString = myFile.ReadToEnd();myFile.Close();// Display the file contents.
Console.WriteLine(myString);
// Suspend the screen.
Console.ReadLine();
private static void ReadFile()
{
using(FileStream fs = new FileStream("d:\\Service.Txt",FileMode.Open,FileAccess.Read))
{
StreamReader sr = new StreamReader(fs,Encoding.Default);
Console.WriteLine(sr.ReadToEnd());
sr.Close();
}
}