打开一个文本文件
1 //打开一个文本文件
2 if (openFileDialog1 .ShowDialog ()== DialogResult .OK )
3 {
4 string file = openFileDialog1.FileName;
5 //如果不加 Encoding.Default,那么StreamReader默认编码为Unicode(UTF-8)
6 StreamReader sr = new StreamReader(file);
7
8 ////增加一System.Text.Encoding.Default 则不传会出现乱码。, System.Text.Encoding.Default
9 //StreamReader sr = new StreamReader(file,Encoding .Default );
10
11 //显示当前流的字符编码
12 //MessageBox.Show(sr.CurrentEncoding.EncodingName);
13
14 string s = sr.ReadLine();
15
16 while (s != null)
17 {
18 textBox1.AppendText(s + "\r\n");
19 s = sr.ReadLine();
20
21 }
22 sr.Close();
23 }
24 }
2 if (openFileDialog1 .ShowDialog ()== DialogResult .OK )
3 {
4 string file = openFileDialog1.FileName;
5 //如果不加 Encoding.Default,那么StreamReader默认编码为Unicode(UTF-8)
6 StreamReader sr = new StreamReader(file);
7
8 ////增加一System.Text.Encoding.Default 则不传会出现乱码。, System.Text.Encoding.Default
9 //StreamReader sr = new StreamReader(file,Encoding .Default );
10
11 //显示当前流的字符编码
12 //MessageBox.Show(sr.CurrentEncoding.EncodingName);
13
14 string s = sr.ReadLine();
15
16 while (s != null)
17 {
18 textBox1.AppendText(s + "\r\n");
19 s = sr.ReadLine();
20
21 }
22 sr.Close();
23 }
24 }