最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#的File類實(shí)現(xiàn)文件操作實(shí)例詳解

 更新時(shí)間:2014年07月29日 10:36:34   投稿:shichen2014  
這篇文章主要介紹了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)行讀寫(xiě)查等基本操作。

具體用法說(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、寫(xiě)文件:

// 創(chuàng)建一個(gè)新文件,在其中寫(xiě)入指定的字節(jié)數(shù)組,然后關(guān)閉該文件。如果目標(biāo)文件已存在,則覆蓋該文件。 
public static void WriteAllBytes(string path, byte[] bytes); 
// 創(chuàng)建一個(gè)新文件,在其中寫(xiě)入一組字符串,然后關(guān)閉該文件。
public static void WriteAllLines(string path, IEnumerable<string> contents); 
// 創(chuàng)建一個(gè)新文件,在其中寫(xiě)入指定的字符串?dāng)?shù)組,然后關(guān)閉該文件。 
public static void WriteAllLines(string path, string[] contents); 
// 使用指定的編碼創(chuàng)建一個(gè)新文件,在其中寫(xiě)入一組字符串,然后關(guān)閉該文件。 
public static void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding); 
// 創(chuàng)建一個(gè)新文件,使用指定的編碼在其中寫(xiě)入指定的字符串?dāng)?shù)組,然后關(guān)閉該文件。 
public static void WriteAllLines(string path, string[] contents, Encoding encoding); 
// 創(chuàng)建一個(gè)新文件,在其中寫(xiě)入指定的字符串,然后關(guān)閉文件。如果目標(biāo)文件已存在,則覆蓋該文件。 
public static void WriteAllText(string path, string contents); 
// 創(chuàng)建一個(gè)新文件,在其中寫(xiě)入指定的字符串,然后關(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è)文件,將指定的字符串寫(xiě)入文件,然后關(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,具有讀/寫(xiě)訪問(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,具有指定的讀、寫(xiě)或讀/寫(xiě)訪問(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);
 // 返回上次寫(xiě)入指定文件或目錄的日期和時(shí)間。 
public static DateTime GetLastWriteTime(string path);
// 返回上次寫(xiě)入指定的文件或目錄的日期和時(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è)置上次寫(xiě)入指定文件的日期和時(shí)間。 
public static void SetLastWriteTime(string path, DateTime lastWriteTime); 
// 設(shè)置上次寫(xiě)入指定的文件的日期和時(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)文章

最新評(píng)論

额尔古纳市| 法库县| 依安县| 武夷山市| 福建省| 平安县| 溧阳市| 吉林省| 雷波县| 东宁县| 盐津县| 洛阳市| 兴义市| 苍南县| 涪陵区| 江津市| 察雅县| 恩平市| 稷山县| 井陉县| 齐齐哈尔市| 绍兴县| 石河子市| 陕西省| 上饶县| 日喀则市| 南靖县| 屏南县| 桃园市| 南江县| 竹溪县| 绥滨县| 崇州市| 曲水县| 新安县| 永川市| 平陆县| 阿瓦提县| 施甸县| 平顶山市| 安远县|