File類,是一個靜態類,主要是來提供一些函數庫用的。靜態實用類,提供了很多靜態的方法,支持對文件的基本操作,包括創建,拷貝,移動,刪除和 打開一個文件。
File類方法的參量很多時候都是路徑path。File的一些方法可以返回FileStream和StreamWriter的對象。可以 和他們配套使用。System.IO.File類和System.IO.FileInfo類
主要提供有關文件的各種操作,在使用時需要引用System.IO命名空間。
一、File類常用的操作方法
1、創建文件方法
//參數1:要創建的文件路徑
File.Create(@"D:\Test\Debug1\測試.txt")
2、打開文件方法
//參數1:要打開的文件路徑,參數2:打開的文件方式
File.Open(@"D:\Test\Debug1\測試.txt",FileMode.Append)
3、追加文件方法
//參數1:要追加的文件路徑,參數2:追加的內容
File.AppendAllText(@"D:\Test\Debug1\測試.txt","哈哈");
4、復制文件方法
//參數1:要復制的源文件路徑,參數2:復制后的目標文件路徑,參數3:是否覆蓋相同文件名
File.Copy(@"D:\Test\Debug1\測試.txt", @"D:\Test\Debug2\測試1.txt", true);
5、移動文件方法
//參數1:要移動的源文件路徑,參數2:移動后的目標文件路徑
File.Move(@"D:\Test\Debug1\測試.txt", @"D:\Test\Debug3\測試2.txt");
6、刪除文件方法
//參數1:要刪除的文件路徑
File.Delete(@"D:\Test\Debug1\測試.txt");
7、設置文件屬性方法
//參數1:要設置屬性的文件路徑,參數2:設置的屬性類型(只讀、隱藏等)
File.SetAttributes(@"D:\Test\Debug1\測試.txt", FileAttributes.Hidden);
二、界面和源碼例子:
1、界面布局
2、源碼例子:
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 FileHandleTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region Files類的文件操作方法(創建、復制、刪除、移動、追加、打開、設置屬性等) static string path = @"D:\Test\Debug1\測試.txt"; //源文件路徑 static string path1 = @"D:\Test\Debug2\測試1.txt"; //文件復制路徑 static string path2 = @"D:\Test\Debug3\測試2.txt"; //文件移動路徑 static string path3 = @"C:\測試3.txt"; //跨盤符存放路徑(測試) /// <summary> /// 1、創建文件方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btncreate_Click(object sender, EventArgs e) { //參數1:指定要判斷的文件路徑 if (!File.Exists(path)) { //參數1:要創建的文件路徑,包含文件名稱、后綴等 FileStream fs = File.Create(path); fs.Close(); MessageBox.Show("文件創建成功!"); } else { MessageBox.Show("文件已經存在!"); } } /// <summary> ///2、 打開文件的方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnopen_Click(object sender, EventArgs e) { if (File.Exists(path)) { //參數1:要打開的文件路徑,參數2:打開的文件方式 FileStream fs = File.Open(path, FileMode.Append); //字節數組 byte[] bytes = { (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o' }; //通過字符流寫入文件 fs.Write(bytes, 0, bytes.Length); fs.Close(); MessageBox.Show("打開並追加Hello成功!"); } else { MessageBox.Show("文件不存在!"); } } /// <summary> /// 3、追加文件內容方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnappend_Click(object sender, EventArgs e) { string appendtext = this.txtContent.Text; if (File.Exists(path)) { //參數1:要追加的文件路徑,參數2:追加的內容 File.AppendAllText(path, appendtext); MessageBox.Show("文件追加內容成功!"); } else { MessageBox.Show("文件不存在!"); } } /// <summary> /// 4、復制文件方法(只能在同個盤符進行操作) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btncopy_Click(object sender, EventArgs e) { if (File.Exists(path)) { //參數1:要復制的源文件路徑,參數2:復制后的目標文件路徑,參數3:是否覆蓋相同文件名 File.Copy(path, path1, true); MessageBox.Show("復制文件成功!"); } else { MessageBox.Show("文件不存在!"); } } /// <summary> /// 5、移動文件方法(只能在同個盤符進行操作) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnmove_Click(object sender, EventArgs e) { if (File.Exists(path)) { //參數1:要移動的源文件路徑,參數2:移動后的目標文件路徑 File.Move(path, path2); MessageBox.Show("移動文件成功!"); } else { MessageBox.Show("文件不存在!"); } } /// <summary> /// 6、刪除文件方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btndelete_Click(object sender, EventArgs e) { if (File.Exists(path)) { //參數1:要刪除的文件路徑 File.Delete(path); MessageBox.Show("文件刪除成功!"); } else { MessageBox.Show("文件不存在!"); } } /// <summary> ////7、設置文件屬性方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnset_Click(object sender, EventArgs e) { if (File.Exists(path)) { //參數1:要設置屬性的文件路徑,參數2:設置的屬性類型(只讀、隱藏等) File.SetAttributes(path, FileAttributes.Hidden); MessageBox.Show("設置文件屬性為隱藏成功!"); } else { MessageBox.Show("文件不存在!"); } } #endregion } }
參考來源:http://www.cnblogs.com/mfc-itblog/p/5771780.html