1. 程式人生 > >C#讀寫文本文件

C#讀寫文本文件

txt text1 空間 box check mes ade message enc

C#中讀寫文本文件.txt文件既可以用File類也可用StreamReader、StreamWrite類。這兩種方法都需要引用using System.IO命名空間。

下面分別給出例子:

1.File類寫入文本文件:

 1         private void btnTextWrite_Click(object sender, EventArgs e)
 2         {
 3             //文件路徑
 4             string filePath = @"E:\123\456.txt";
 5 
 6             //檢測文件夾是否存在,不存在則創建
7 NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly)); 8 9 //定義編碼方式,text1.Text為文本框控件中的內容 10 byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text); 11 string mystr1 = Encoding.UTF8.GetString(mybyte);
12 13 //寫入文件 14 //File.WriteAllBytes(filePath,mybyte);//寫入新文件 15 //File.WriteAllText(filePath, mystr1);//寫入新文件 16 File.AppendAllText(filePath, mystr1);//添加至文件 17 18 }

2.File類讀取文本文件:

 1         private void btnTexRead_Click(object sender, EventArgs e)
2 { 3 //文件路徑 4 string filePath = @"E:\123\456.txt"; 5 try 6 { 7 if (File.Exists(filePath)) 8 { 9 text1.Text = File.ReadAllText(filePath); 10 byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text); 11 text1.Text = Encoding.UTF8.GetString(mybyte); 12 } 13 else 14 { 15 MessageBox.Show("文件不存在"); 16 } 17 } 18 catch (Exception ex) 19 { 20 MessageBox.Show(ex.Message); 21 } 22 }

3.StreamWrite類寫入文本文件:

 1         private void btnTextWrite_Click(object sender, EventArgs e)
 2         {
 3             //文件路徑
 4             string filePath = @"E:\123\456.txt";
 5 
 6             try
 7             {
 8                 //檢測文件夾是否存在,不存在則創建
 9                 string mystr1 = NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly));
10 
11                 using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8))
12                 {
13                     byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
14                     text1.Text = Encoding.UTF8.GetString(mybyte);
15                     sw.Write(text1.Text);
16                 }
17 
18             }
19             catch
20             {
21 
22             }
23         }

4.StreamReader類讀取文本文檔:

 1         private void btnTexRead_Click(object sender, EventArgs e)
 2         {
 3             //文件路徑
 4             string filePath = @"E:\123\456.txt";
 5             try
 6             {
 7                 if (File.Exists(filePath))
 8                 {
 9                     using (StreamReader sr = new StreamReader(filePath, Encoding.UTF8))
10                     {
11                         text1.Text = sr.ReadToEnd();
12                         byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
13                         text1.Text = Encoding.UTF8.GetString(mybyte);
14                     }
15                 }
16                 else
17                 {
18                     MessageBox.Show("文件不存在");
19                 }
20             }
21             catch (Exception ex)
22             {
23                 MessageBox.Show(ex.Message);
24             }
25         }

C#讀寫文本文件