此篇文檔是老早就放到草稿箱中了,斷斷續續編輯了幾次。結合自己經常用到的對於文件的操作,結合msdn做此文。
1、File 類 --轉自msdn
常用方法:
名稱 | 說明 |
---|---|
AppendAllLines(String, IEnumerable<String>) |
向一個文件中追加行,然后關閉該文件。 如果指定文件不存在, 此方法會創建一個文件,向其中寫入指定的行,然后關閉該文件。 |
AppendAllLines(String, IEnumerable<String>, Encoding) |
使用指定的編碼向一個文件中追加行,然后關閉該文件。 如果指 定文件不存在,此方法會創建一個文件,向其中寫入指定的行,然 后關閉該文件。 |
AppendAllText(String, String) | 打開一個文件,向其中追加指定的字符串,然后關閉該文件。 如果文件不存在,此方法將創建一個文件,將指定的字符串寫 入文件,然后關閉該文件。 |
Create(String) | 在指定路徑中創建或覆蓋文件。 |
Create(String, Int32) | 創建或覆蓋指定的文件。(string path, int bufferSize) |
Exists(String) | 確定指定的文件是否存在。bool |
Open(String, FileMode) | 以讀/寫訪問權限打開指定路徑上的 FileStream。 |
ReadAllLines(String) | 打開一個文本文件,讀取文件的所有行,然后關閉該文件。 |
ReadAllText(String) | 打開一個文本文件,讀取文件的所有行,然后關閉該文件。 |
|
創建一個新文件,向其中寫入指定的字符串,然后關閉文件。 如果目標文件已存在,則覆蓋該文件。 |
WriteAllLines(String, IEnumerable<String>) | 創建一個新文件,向其中寫入一個字符串集合,然后關閉該文件。 |
使用File類用於典型操作,如復制、移動、重命名、創建、打開、刪除和一次將追加到單個文件。您(這個字保留,敬親愛的讀者)還可以使用File類用於獲取和設置文件屬性或DateTime與創建過程中,相關信息訪問及文件的寫入。如果您想要在多個文件上執行操作,請參閱Directory.GetFiles 或 DirectoryInfo.GetFiles。
2、FileInfo 類 --轉自msdn
提供用於創建、復制、刪除、移動和打開文件的屬性和實例方法,並且幫助創建 FileStream 對象。 此類不能被繼承。
名稱 | 說明 |
---|---|
FileInfo(String) |
初始化作為文件路徑的包裝的 FileInfo 類的新實例。 |
屬性
名稱 | 說明 | 語法 |
---|---|---|
CreationTime |
獲取或設置當前文件或目錄的創建時間。 (繼承自 FileSystemInfo。) |
|
Directory |
獲取父目錄的實例。 |
public DirectoryInfo Directory { get; } |
DirectoryName |
獲取表示目錄的完整路徑的字符串。 |
public string DirectoryName { get; } |
Exists |
獲取指示文件是否存在的值。 (覆蓋 FileSystemInfo.Exists。) |
public override bool Exists { get; } |
Extension |
獲取表示文件擴展名部分的字符串。 (繼承自 FileSystemInfo。) |
public string Extension { get; } |
FullName |
獲取目錄或文件的完整目錄。 (繼承自 FileSystemInfo。) |
public virtual string FullName { get; } |
Name | 獲取文件名。 (覆蓋 FileSystemInfo.Name。)
|
常用方法:
名稱 | 說明 |
---|---|
AppendText() |
創建一個 StreamWriter,它向 FileInfo 的此實例表示的文件追加文本。 |
CopyTo(String) |
將現有文件復制到新文件,不允許覆蓋現有文件。 |
Create() | 創建文件。 |
OpenText() | 創建使用從現有文本文件中讀取的 UTF8 編碼的 StreamReader。 |
3、Directory 類 --轉自msdn
公開用於通過目錄和子目錄進行創建、移動和枚舉的靜態方法。 無法繼承此類。
常用方法
名稱 | 說明 | 備注 |
---|---|---|
CreateDirectory(String) |
在指定路徑中創建所有目錄和子目錄,除非它們已經存在。 |
中指定的所有目錄 path 創建的除非它們已經存在,或除非的某些部分 path 無效。 如果該目錄已存在,此方法不會創建一個新目錄,但它將返回 DirectoryInfo 現有目錄的對象。 path 參數指定目錄路徑,而不是文件路徑 |
CreateDirectory(String, DirectorySecurity) |
在指定路徑中創建所有目錄(除非已存在),並應用指定的 Windows 安全性。 |
|
Delete(String) |
從指定路徑刪除空目錄。 |
|
Delete(String, Boolean) |
刪除指定的目錄,並刪除該目錄中的所有子目錄和文件(如果表示)。 |
|
Exists(String) | 確定給定路徑是否引用磁盤上的現有目錄。 | 如果 path 指向現有目錄,則為 true; 如果該目錄不存在或者在嘗試確定指定目 錄是否存在時出錯,則為 false。 |
GetCurrentDirectory() | 獲取應用程序的當前工作目錄。 | 包含當前工作目錄的路徑且不以反斜杠 (\) 結尾的字符串。 |
名稱 | 說明 | 語法 |
---|---|---|
Exists |
獲取指示目錄是否存在的值。(覆蓋 FileSystemInfo.Exists。) |
public override bool Exists { get; } |
Extension | 獲取表示文件擴展名部分的字符串。(繼承自 FileSystemInfo。) | |
FullName | 獲取目錄的完整路徑。(覆蓋 FileSystemInfo.FullName。) | |
Name | 獲取此 DirectoryInfo 實例的名稱。(覆蓋 FileSystemInfo.Name。) | |
Parent | 獲取指定的子目錄的父目錄。 | |
Root | 獲取目錄的根部分。 |
方法
名稱 | 說明 | 語法 |
---|---|---|
Create() |
創建目錄。 |
|
Create(DirectorySecurity) |
使用 DirectorySecurity 對象創建目錄。 |
|
Delete() | 如果此 DirectoryInfo 為空則將其刪除。(覆蓋 FileSystemInfo.Delete()。) | |
Delete(Boolean) | 刪除 DirectoryInfo 的此實例,指定是否刪除子目錄和文件。 | |
GetFiles() | 返回當前目錄的文件列表。(FileInfo 類型的數組。) | di.GetFiles() |
GetFiles(String) | 返回當前目錄中與給定的搜索模式匹配的文件列表。(FileInfo 類型的數組。) | di.GetFiles("*2*") di.GetFiles("test?.txt") |
綜合應用的例子

1 class FileOperate 2 { 3 /// <summary> 4 /// 構造函數 5 /// </summary> 6 public FileOperate() 7 { 8 } 9 10 /// <summary> 11 /// FileOperate1()的重載 12 /// 理解構造函數 13 /// </summary> 14 public void FileOperate1() 15 { 16 } 17 public static void Main() 18 { 19 try 20 { 21 FileOperate fo = new FileOperate(); 22 #region 文件夾操作:是否存在,創建,刪除 23 //string path = @"D:\MrSong1"; //正常路徑 24 //string illeglePath = @"D:\111>"; //含有非法字符 25 //string overLengthPath = @"" + fo.GetLongPath(); //路徑超長 26 27 //fo.DirectoryNomal(path); //正常 28 29 //fo.DirectoryNomal(illeglePath); //報“路徑中存在非法字符異常”,下面的方法不執行 30 31 //fo.DirectoryNomal(overLengthPath); //修改illegalPath為正確路徑后,執行此方法,報”指定路徑名過長“ 32 #endregion 33 34 #region 文件操作 35 string filePath = @"D:\11.txt"; 36 fo.FileOperate1(filePath); 37 File.Delete(filePath); //刪除 38 #endregion 39 40 } 41 catch (Exception ex) 42 { 43 Console.WriteLine(ex); 44 } 45 Console.ReadKey(); 46 } 47 48 /// <summary> 49 /// 文件夾操作 50 /// </summary> 51 /// <param name="path"></param> 52 private void DirectoryNomal(string path) 53 { 54 if (Directory.Exists(path)) 55 { 56 Console.WriteLine("Directory類方法判斷:存在路徑"); 57 Console.WriteLine("刪除:" + path); 58 Directory.Delete(path); //刪除 59 } 60 else 61 { 62 Console.WriteLine("Directory類方法判斷:不存在" + path); 63 Console.WriteLine("創建:" + path); 64 Directory.CreateDirectory(path); //創建 65 } 66 DirectoryInfo dirInfo = new DirectoryInfo(path); 67 if (dirInfo.Exists) 68 { 69 Console.WriteLine("DirectoryInfo屬性判斷:存在路徑" + path); 70 Console.WriteLine("DirectoryInfo刪除:" + path); 71 dirInfo.Delete(); 72 } 73 else 74 { 75 Console.WriteLine("DirectoryInfo屬性判斷:不存在路徑"); 76 Console.WriteLine("DirectoryInfo創建:" + path); 77 dirInfo.Create(); //創建路徑 78 } 79 } 80 81 /// <summary> 82 /// 獲取長文件夾名稱 83 /// </summary> 84 /// <returns></returns> 85 private string GetLongPath() 86 { 87 string path = ""; 88 char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; 89 System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks)); 90 int i = 0; 91 int n = Pattern.Length; 92 93 while (i <= 257) 94 { 95 int rnd = random.Next(0, n); 96 path += Pattern[rnd]; 97 i++; 98 } 99 return path; 100 } 101 102 /// <summary> 103 /// 文件操作 104 /// </summary> 105 /// <param name="path"></param> 106 public void FileOperate1(string path) 107 { 108 if (File.Exists(path)) 109 { 110 Console.WriteLine(path + "文件存在"); 111 File.Delete(path); 112 } 113 else 114 { 115 Console.WriteLine(path + "文件不存在"); 116 Console.WriteLine("創建該文件!"); 117 File.Create(path); 118 } 119 File.WriteAllLines(path, new string[] { "Hello World!" }); 120 } 121 }
注意:
1、Exists是Directory類的方法,在DirectoryInfo中是屬性。
2、DirectoryInfo類初始化(構造函數)中會報異常,有非法字符串,超長,path為null,調用方沒有權限錯誤。