using System; using System.IO;class Test { public static void Main() { try { // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (StreamReader sr = new StreamReader("TestFile.txt")) { String line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } } }
using (StreamReader vStreamReader = new StreamReader( @"c:\temp\temp.txt", Encoding.Default)) textBox1.Text = vStreamReader.ReadToEnd();
StreamReader sr = new StreamReader("C:\\tmp.txt"); string s = sr..ReadToEnd(); sr.Close();
StreamReader sr = new StreamReader("C:\\tmp.txt"); string s = sr.ReadToEnd(); sr.Close();
using(StreamReader sr = new StreamReader("全路径文件名")) { // sr.ReadToEnd() 就是文件内容的string }
using System.IO;class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
@"c:\temp\temp.txt", Encoding.Default))
textBox1.Text = vStreamReader.ReadToEnd();
string s = sr..ReadToEnd();
sr.Close();
string s = sr.ReadToEnd();
sr.Close();
{
// sr.ReadToEnd() 就是文件内容的string
}
FileStream fs = new FileStream ( "C:\\file.txt" , FileMode.Open , FileAccess.Read ) ;
StreamReader m_streamReader = new StreamReader ( fs ) ;
//使用StreamReader类来读取文件
m_streamReader.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;
// 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
this.richTextBox1.Text = "" ;
string strLine = m_streamReader.ReadLine ( ) ;
while ( strLine != null )
{
this.richTextBox1.Text += strLine + "\n" ;
strLine = m_streamReader.ReadLine ( ) ;
}
//关闭此StreamReader对象
m_streamReader.Close ( ) ;