文章出處
文章列表
文件讀寫:需要實現的功能是:點擊1,彈出文件打開對話框OpenFiledialog,選擇要讀取的文件,點擊確定之后,把文件的路徑顯示在1上,然后點擊讀取,把文件的內容顯示在3上面;
同理,文件的寫入,我們在3中寫好內容之后,點擊2,彈出文件保存對話框SaveFiledialog,然后選擇好路徑之后,點擊寫入,就將我們在3中寫好的內容,保存在指定的路徑了。
現在實現功能:
private void txtReadPath_Click(object sender, EventArgs e) { //打開文件對話框 using (OpenFileDialog openFileDialog = new OpenFileDialog()) { if (openFileDialog.ShowDialog() == DialogResult.OK) { //顯示文件的全路徑 txtReadPath.Text = openFileDialog.FileName; } } } /// <summary> /// 讀取文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnNewRead_Click(object sender, EventArgs e) { //創建文件流(讀文件的時候,FileMode選擇Open) using (FileStream fileRead = new FileStream(txtReadPath.Text,FileMode.Open)) { //創建btpe數組 byte [] fileByte=new byte[fileRead.Length]; //把文件數據讀取到數組中 fileRead.Read(fileByte, 0, fileByte.Length); //現在將數據轉化為string格式的數據,好顯示在txt控件中,通過下面的方法把byte數組轉化為string字符串 txtContent.Text= Encoding.Default.GetString(fileByte); } }
這部分就實現了讀取文件的數據到文本框中,其中Encoding.Default.GetString這個方法只能,讀取文本文件。不能讀取Word,所謂文本文件就是可以用記事本打開的。
Encoding.Default.GetString這個方法還有個問題,會亂碼:

解決方法:
txtContent.Text = Encoding.GetEncoding("utf-8").GetString(fileByte);
修正之后:

好了,下面開始文件的寫入功能實現:




步驟:我們先在文本框中輸入內容,然后點擊第二個文本框,彈出保存對話框,我們選擇路徑,輸入要保存的名字(帶上后綴)。然后點擊保存。
所有的代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace FileReadDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void txtReadPath_Click(object sender, EventArgs e) { //打開文件對話框 using (OpenFileDialog openFileDialog = new OpenFileDialog()) { if (openFileDialog.ShowDialog() == DialogResult.OK) { //顯示文件的全路徑 txtReadPath.Text = openFileDialog.FileName; } } } /// <summary> /// 讀取文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnNewRead_Click(object sender, EventArgs e) { //創建文件流(讀文件的時候,FileMode選擇Open) using (FileStream fileRead = new FileStream(txtReadPath.Text,FileMode.Open)) { //創建btpe數組 byte [] fileByte=new byte[fileRead.Length]; //把文件數據讀取到數組中 fileRead.Read(fileByte, 0, fileByte.Length); //現在將數據轉化為string格式的數據,好顯示在txt控件中,通過下面的方法把byte數組轉化為string字符串 //txtContent.Text= Encoding.Default.GetString(fileByte); //這個方法會亂碼、 txtContent.Text = Encoding.GetEncoding("utf-8").GetString(fileByte); } } /// <summary> /// 文件寫入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textWritePath_Click(object sender, EventArgs e) { //創建文件保存對話框 using (SaveFileDialog saveFileDialog = new SaveFileDialog()) { if (saveFileDialog.ShowDialog() == DialogResult.OK) { //全路徑 textWritePath. Text = saveFileDialog.FileName; } } } private void btnWrite_Click(object sender, EventArgs e) { //將內容寫入到指定的路徑文件中 using (FileStream fileWrite = new FileStream(textWritePath.Text.Trim(),FileMode.Create)) { //Write方法,需要傳入Byte數組,這個時候,就有個問題了,我們在文本控件中,輸入的內容是字符串的,不過同理,字串串也可以轉化為byte數組 byte [] fileWriteByte=Encoding.GetEncoding("utf-8").GetBytes(txtContent.Text.Trim()); fileWrite.Write(fileWriteByte, 0, fileWriteByte.Length); } } } }
文章列表
全站熱搜