C#的File類實(shí)現(xiàn)文件操作實(shí)例詳解
C#對(duì)文件的操作相當(dāng)方便,主要涉及到四個(gè)類:File、FileInfo、Directory、DirectoryInfo,前兩個(gè)提供了針對(duì)文件的操作,后兩個(gè)提供了針對(duì)目錄的操作,類圖關(guān)系如下:
本文舉例詳述了File類的用法。File中提供了許多的靜態(tài)方法,使用這些靜態(tài)方法我們可以方便的對(duì)文件進(jìn)行讀寫查等基本操作。
具體用法說(shuō)明及相關(guān)示例如下:
1、讀文件:
// 打開(kāi)一個(gè)文件,將文件的內(nèi)容讀入一個(gè)字符串,然后關(guān)閉該文件。 public static byte[] ReadAllBytes(string path); // 打開(kāi)一個(gè)文本文件,讀取文件的所有行,然后關(guān)閉該文件。 public static string[] ReadAllLines(string path); // 打開(kāi)一個(gè)文件,使用指定的編碼讀取文件的所有行,然后關(guān)閉該文件。 public static string[] ReadAllLines(string path, Encoding encoding); // 打開(kāi)一個(gè)文本文件,讀取文件的所有行,然后關(guān)閉該文件。 public static string ReadAllText(string path); // 打開(kāi)一個(gè)文件,使用指定的編碼讀取文件的所有行,然后關(guān)閉該文件。 public static string ReadAllText(string path, Encoding encoding); // 讀取文件的文本行。 public static IEnumerable<string> ReadLines(string path); // 讀取具有指定編碼的文件的文本行。 public static IEnumerable<string> ReadLines(string path, Encoding encoding);
2、寫文件:
// 創(chuàng)建一個(gè)新文件,在其中寫入指定的字節(jié)數(shù)組,然后關(guān)閉該文件。如果目標(biāo)文件已存在,則覆蓋該文件。 public static void WriteAllBytes(string path, byte[] bytes); // 創(chuàng)建一個(gè)新文件,在其中寫入一組字符串,然后關(guān)閉該文件。 public static void WriteAllLines(string path, IEnumerable<string> contents); // 創(chuàng)建一個(gè)新文件,在其中寫入指定的字符串?dāng)?shù)組,然后關(guān)閉該文件。 public static void WriteAllLines(string path, string[] contents); // 使用指定的編碼創(chuàng)建一個(gè)新文件,在其中寫入一組字符串,然后關(guān)閉該文件。 public static void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding); // 創(chuàng)建一個(gè)新文件,使用指定的編碼在其中寫入指定的字符串?dāng)?shù)組,然后關(guān)閉該文件。 public static void WriteAllLines(string path, string[] contents, Encoding encoding); // 創(chuàng)建一個(gè)新文件,在其中寫入指定的字符串,然后關(guān)閉文件。如果目標(biāo)文件已存在,則覆蓋該文件。 public static void WriteAllText(string path, string contents); // 創(chuàng)建一個(gè)新文件,在其中寫入指定的字符串,然后關(guān)閉文件。如果目標(biāo)文件已存在,則覆蓋該文件。 public static void WriteAllText(string path, string contents, Encoding encoding);
3、追加內(nèi)容:
// 在一個(gè)文件中追加文本行,然后關(guān)閉該文件。 public static void AppendAllLines(string path, IEnumerable<string> contents); // 使用指定的編碼向一個(gè)文件中追加文本行,然后關(guān)閉該文件。 public static void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding); // 打開(kāi)一個(gè)文件,向其中追加指定的字符串,然后關(guān)閉該文件。如果文件不存在,此方法創(chuàng)建一個(gè)文件,將指定的字符串寫入文件,然后關(guān)閉該文件。 public static void AppendAllText(string path, string contents); // 將指定的字符串追加到文件中,如果文件還不存在則創(chuàng)建該文件。 public static void AppendAllText(string path, string contents, Encoding encoding); // 創(chuàng)建一個(gè) System.IO.StreamWriter,它將 UTF-8 編碼文本追加到現(xiàn)有文件。 public static StreamWriter AppendText(string path);
4、創(chuàng)建文件:
// 在指定路徑中創(chuàng)建或覆蓋文件。 public static FileStream Create(string path); // 創(chuàng)建或覆蓋指定的文件。 public static FileStream Create(string path, int bufferSize); // 創(chuàng)建或覆蓋指定的文件,并指定緩沖區(qū)大小和一個(gè)描述如何創(chuàng)建或覆蓋該文件的 System.IO.FileOptions 值。 public static FileStream Create(string path, int bufferSize, FileOptions options); // 創(chuàng)建或覆蓋具有指定的緩沖區(qū)大小、文件選項(xiàng)和文件安全性的指定文件。 public static FileStream Create(string path, int bufferSize, FileOptions options, FileSecurity fileSecurity);
5、打開(kāi)文件:
// 打開(kāi)指定路徑上的 System.IO.FileStream,具有讀/寫訪問(wèn)權(quán)限。 public static FileStream Open(string path, FileMode mode); // 以指定的模式和訪問(wèn)權(quán)限打開(kāi)指定路徑上的 System.IO.FileStream。 public static FileStream Open(string path, FileMode mode, FileAccess access); // 打開(kāi)指定路徑上的 System.IO.FileStream,具有指定的讀、寫或讀/寫訪問(wèn)模式以及指定的共享選項(xiàng)。 public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share); // 打開(kāi)現(xiàn)有文件以進(jìn)行讀取。 public static FileStream OpenRead(string path);
6、獲取和設(shè)置文件屬性:
// 獲取一個(gè) System.Security.AccessControl.FileSecurity 對(duì)象,它封裝指定文件的訪問(wèn)控制列表 (ACL) 條目。 public static FileSecurity GetAccessControl(string path); // 獲取一個(gè) System.Security.AccessControl.FileSecurity 對(duì)象,它封裝特定文件的指定類型的訪問(wèn)控制列表 (ACL)項(xiàng)。 public static FileSecurity GetAccessControl(string path, AccessControlSections includeSections); // 獲取在此路徑上的文件的 System.IO.FileAttributes。 public static FileAttributes GetAttributes(string path); // 返回指定文件或目錄的創(chuàng)建日期和時(shí)間。 public static DateTime GetCreationTime(string path); // 返回指定的文件或目錄的創(chuàng)建日期及時(shí)間,其格式為協(xié)調(diào)世界時(shí) (UTC)。 public static DateTime GetCreationTimeUtc(string path); // 返回上次訪問(wèn)指定文件或目錄的日期和時(shí)間。 public static DateTime GetLastAccessTime(string path); // 返回上次訪問(wèn)指定的文件或目錄的日期及時(shí)間,其格式為協(xié)調(diào)世界時(shí) (UTC)。 public static DateTime GetLastAccessTimeUtc(string path); // 返回上次寫入指定文件或目錄的日期和時(shí)間。 public static DateTime GetLastWriteTime(string path); // 返回上次寫入指定的文件或目錄的日期和時(shí)間,其格式為協(xié)調(diào)世界時(shí) (UTC)。 public static DateTime GetLastWriteTimeUtc(string path); // 對(duì)指定的文件應(yīng)用由 System.Security.AccessControl.FileSecurity 對(duì)象描述的訪問(wèn)控制列表 (ACL) 項(xiàng)。 public static void SetAccessControl(string path, FileSecurity fileSecurity); // 設(shè)置指定路徑上文件的指定的 System.IO.FileAttributes。 public static void SetAttributes(string path, FileAttributes fileAttributes); // 設(shè)置創(chuàng)建該文件的日期和時(shí)間。 public static void SetCreationTime(string path, DateTime creationTime); // 設(shè)置文件創(chuàng)建的日期和時(shí)間,其格式為協(xié)調(diào)世界時(shí) (UTC)。 public static void SetCreationTimeUtc(string path, DateTime creationTimeUtc); // 設(shè)置上次訪問(wèn)指定文件的日期和時(shí)間。 public static void SetLastAccessTime(string path, DateTime lastAccessTime); // 設(shè)置上次訪問(wèn)指定的文件的日期和時(shí)間,其格式為協(xié)調(diào)世界時(shí) (UTC)。 public static void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc); // 設(shè)置上次寫入指定文件的日期和時(shí)間。 public static void SetLastWriteTime(string path, DateTime lastWriteTime); // 設(shè)置上次寫入指定的文件的日期和時(shí)間,其格式為協(xié)調(diào)世界時(shí) (UTC)。 public static void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc);
7、復(fù)制、移動(dòng)、替換:
// 將現(xiàn)有文件復(fù)制到新文件。不允許覆蓋同名的文件。 public static void Copy(string sourceFileName, string destFileName); // 將現(xiàn)有文件復(fù)制到新文件。允許覆蓋同名的文件。 public static void Copy(string sourceFileName, string destFileName, bool overwrite); // 將指定文件移到新位置,并提供指定新文件名的選項(xiàng)。 public static void Move(string sourceFileName, string destFileName); // 使用其他文件的內(nèi)容替換指定文件的內(nèi)容,這一過(guò)程將刪除原始文件,并創(chuàng)建被替換文件的備份。 public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName); // 用其他文件的內(nèi)容替換指定文件的內(nèi)容,刪除原始文件,并創(chuàng)建被替換文件的備份和(可選)忽略合并錯(cuò)誤。 public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);
8、加密解密、刪除、判定是否存在:
// 將某個(gè)文件加密,使得只有加密該文件的帳戶才能將其解密。 public static void Encrypt(string path); // 解密由當(dāng)前帳戶使用 System.IO.File.Encrypt(System.String) 方法加密的文件。 public static void Decrypt(string path); // 刪除指定的文件。如果指定的文件不存在,則不引發(fā)異常 public static void Delete(string path); // 確定指定的文件是否存在。 public static bool Exists(string path);
總結(jié):
通過(guò)上面的函數(shù)聲明及相關(guān)注釋說(shuō)明,相信大家應(yīng)該很清楚如何是好這些方法了。同時(shí),看到如此多的函數(shù),我們也很清楚的知道,F(xiàn)ile類已經(jīng)可以滿足我們對(duì)文件操作的基本需求。
這里還需要注意一點(diǎn):File類通過(guò)靜態(tài)方法的方式為我們提供了操作文件的途徑。
相關(guān)文章
C#創(chuàng)建一個(gè)小型Web Server(Socket實(shí)現(xiàn))
這篇文章主要介紹了關(guān)于C#利用Socket實(shí)現(xiàn)創(chuàng)建一個(gè)小型Web Server的相關(guān)資料,文中通過(guò)示例代碼介紹的很詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-02-02C#實(shí)現(xiàn)的優(yōu)酷真實(shí)視頻地址解析功能(2014新算法)
這篇文章主要介紹了C#實(shí)現(xiàn)的優(yōu)酷真實(shí)視頻地址解析功能(2014新算法),本文在當(dāng)前環(huán)境下是有效的,因?yàn)閮?yōu)酷之前更新了算法,需要的朋友可以參考下2014-10-10C#使用Protocol Buffer(ProtoBuf)進(jìn)行Unity中的Socket通信
這篇文章主要介紹了C#使用Protocol Buffer(ProtoBuf)進(jìn)行Unity的Socket通信的實(shí)例,Protocol Buffer是Google開(kāi)發(fā)的數(shù)據(jù)格式,也是除了XML和JSON之外人氣第三高的^^需要的朋友可以參考下2016-04-04C#實(shí)現(xiàn)按數(shù)據(jù)庫(kù)郵件列表發(fā)送郵件的方法
這篇文章主要介紹了C#實(shí)現(xiàn)按數(shù)據(jù)庫(kù)郵件列表發(fā)送郵件的方法,涉及C#讀取數(shù)據(jù)庫(kù)及通過(guò)自定義函數(shù)發(fā)送郵件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07C#使用selenium實(shí)現(xiàn)操作瀏覽器并且截圖
這篇文章主要為大家詳細(xì)介紹了C#如何使用selenium組件實(shí)現(xiàn)操作瀏覽器并且截圖,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴可以參考一下2024-01-01