【C#】201801013關於檔案的操作
阿新 • • 發佈:2018-12-15
1、基於流的文字檔案的讀寫對於C#來說比較簡單,通常來講,需要以下5個基本步驟:
- 建立一個檔案流——FileStream
- 建立讀取器或寫入器——StreamReader/StreamWriter
- 執行讀或者寫操作
- 關閉讀取器或寫入器
- 關閉檔案流
2、BinaryReader和BinaryWriter類可用於讀寫二進位制檔案 這兩個類並不派生於Stream,而是直接派生於System.Object類 所有建立兩個類的物件必須基於所提供的派生於Stream類的物件,如FileStream類的物件
3、File類和FileInfo類——檔案基本操作 Directory類和DirectoryInfo類——目錄基本操作 Path類和Enviroment類——路徑類和系統資訊類
4、當要讀取的檔案中包含漢字,需要指定編碼Encoding.GetEncoding(“GB2312”) string[ ] content=File.ReadAllLines(path,Encoding.GetEncoding(“GB2312”));
5、讀寫文字檔案
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Text; namespace WindowsFormsApp4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void btnRead_Click(object sender, EventArgs e) { if (ofdOpen.ShowDialog() == DialogResult.OK) { string filePath = ofdOpen.FileName; txtFilePath.Text = filePath; if (File.Exists(filePath)) { StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("gb2312")); string input; while ((input = sr.ReadLine()) != null) { txtFileContent.Text += input + "\r\n"; } sr.Close(); } else { MessageBox.Show("您要讀取的檔案不存在!"); } } } private void btnWrite_Click(object sender, EventArgs e) { sfdSave.Filter = "文字檔案(*.txt)|*.txt"; if (sfdSave.ShowDialog() == DialogResult.OK) { string filePath = sfdSave.FileName; StreamWriter sw = new StreamWriter(filePath, false); sw.WriteLine(txtFileContent.Text); sw.Close(); } } } }
6、檔案基本操作
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using Microsoft.VisualBasic.Devices; namespace WindowsFormsApp3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } private void btnCreate_Click(object sender, EventArgs e) { string path = @txtFile.Text; if (File.Exists(path)) MessageBox.Show("檔名已存在"); try { using(StreamWriter sw = File.CreateText(path)) { sw.WriteLine("不想說話"); sw.WriteLine(""); sw.WriteLine("建立日期時間"+DateTime.Now.ToString()); sw.Flush(); MessageBox.Show("檔案建立成功!"); } } catch(Exception ex) { MessageBox.Show("檔案無法建立。" + Environment.NewLine + "請確認檔名稱是否正確," + "以及您是否擁有建立許可權。"); } } private void btnCopy_Click(object sender, EventArgs e) { try { File.Copy(@"d:\t87.txt", @"d:\t87_1.txt", true); FileInfo fi = new FileInfo(@"d:\t87.txt"); fi.CopyTo(@"d:\t87_2.txt", true); MessageBox.Show("檔案已複製"); } catch(Exception ex) { MessageBox.Show(ex.Message); return; } } private void btnRename_Click(object sender, EventArgs e) { try { Computer MyComputer = new Computer(); // File.Copy(@"d:\t87.text", @"d:\t87_3.text", true); MyComputer.FileSystem.RenameFile(@"d:\t87.txt", "更改檔名.txt"); MessageBox.Show("更改檔名成功!"); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } } private void btnMove_Click(object sender, EventArgs e) { if(File.Exists(@"d:\t87.txt")){ //檔案被執行緒佔用,不能移動,解決方法可以重新Copy一個移動 File.Copy(@"d:\t87.txt", @"d:\移動.txt",true); File.Move(@"d:\移動.txt", @"d:\移動1.txt"); MessageBox.Show("檔案移動成功!"); } else { FileInfo fi = new FileInfo(@"d:\t87.txt"); StreamWriter sw = fi.CreateText(); MessageBox.Show("移動前例項化檔案" + fi.Name); //檔案被執行緒佔用,不能移動 fi.CopyTo(@"d:\移動2.txt"); } } private void btnDelete_Click(object sender, EventArgs e) { try { if (File.Exists(@"d:\t87.txt")) File.Delete(@"d:\t87.txt"); else { FileInfo fi = new FileInfo(@"d:\t87.txt"); StreamWriter sw = fi.CreateText(); MessageBox.Show("刪除前例項化檔案" + fi.Name); } MessageBox.Show("檔案刪除成功!"); } catch(Exception ex) { MessageBox.Show(ex.Message); return; } } } }