.Net那點事兒系列:System.IO之windows檔案操作

來源:互聯網
上載者:User

System.IO系列目錄

1.Windows檔案目錄處理

2.Stream 以及 StreamReader和StreamWriter

3.壓縮檔

4.System.IO之記憶體對應檔共用記憶體

5.System.IO系列:區域網路內多線程使用具名管道在進程之間通訊執行個體 System.IO之使用管道在處理序間通訊 (System.IO.Pipes使用)

6.隔離儲存區 (Isolated Storage)區

7.連接埠

在.Net中處理系統檔案相關的幾個類分別是File、Directory、FileInfo、DirectoryInfo、DriveInfo、FileSystemWatcher。本文介紹下這幾個類的用法。

1.File類提供靜態方法用來建立、移動、複製、刪除檔案的操作,並可以開啟檔案流

2.Directory類提供靜態方法用來建立、移動、複製、刪除目錄的操作

3.FileInfo類用類執行個體實現建立、複製、移動、刪除檔案的操作

4.DirectoryInfo提供建立、移動、複製、刪除目錄的操作,並可以枚舉子目錄

5.DriveInfo可以獲得windows作業系統中的磁碟資訊

6.FileSystemWatcher用來監視檔案或目錄變化,並引發事件

7.Path類提供檔案名稱目錄名操作的靜態方法

File、FileInfo、Directory、DirectoryInfo這幾個類的使用方法都非常簡單就不做贅述了。

1.如何使用DriveInfo獲得windows系統磁碟資訊

不允許在程式中自己構造DriveInfo的執行個體,可以通過DriveInfo的靜態方法GetDrives()獲得windows系統中所有的磁碟,包括硬碟,cd以及u盤;注意在訪問磁碟屬性時需要先判斷其IsReady屬性是否為true,IsReady為false時訪問磁碟的一些屬性時會拋出異常。如下執行個體代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace aboutio{    class Program    {        static void Main(string[] args)        {            DriveInfo[] drives = DriveInfo.GetDrives();            foreach (DriveInfo drive in drives)            {                if(drive.IsReady)                    Console.WriteLine("類型:{0} 卷標:{1} 名稱:{2} 總空間:{3} 剩餘空間:{4}",drive.DriveType, drive.VolumeLabel,drive.Name,drive.TotalSize,drive.TotalFreeSpace);                else                    Console.WriteLine("類型:{0}  is not ready",drive.DriveType);            }            Console.ReadLine();        }    }}

2. 使用FileSystemWatcher監視目錄

FileSystemWatcher用來監視目錄或者檔案的修改,建立,刪除,要使FileSystemWatcher開始監視必須設定其EnableRaisingEvents屬性為true,如下樣本:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Threading;namespace UseFileSystemWatcher{    class Program    {        static void Main(string[] args)        {            //聲明要監視的目錄            string watchPath = "D:\\watch";            FileSystemWatcher watcher = new FileSystemWatcher(watchPath, "*.*");            //添加檔案變化處理事件            watcher.Changed += new FileSystemEventHandler(Watcher_Changed);            //添加檔案建立處理事件            watcher.Created += new FileSystemEventHandler(Watcher_Created);            //添加檔案刪除處理事件            watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);                        //添加錯誤處理            watcher.Error += new ErrorEventHandler(Watcher_Error);            //啟動監視            watcher.EnableRaisingEvents = true;            Thread.Sleep(1000 * 60);            Console.WriteLine("press any key to exit..");            Console.Read();        }        static void Watcher_Error(object sender, ErrorEventArgs e)        {            Console.WriteLine("錯誤:" + e.ToString());        }        static void Watcher_Deleted(object sender, FileSystemEventArgs e)        {            Console.WriteLine(e.ChangeType + ":" + e.FullPath);        }        static void Watcher_Created(object sender, FileSystemEventArgs e)        {            Console.WriteLine(e.ChangeType + ":" + e.FullPath);        }        static void Watcher_Changed(object sender, FileSystemEventArgs e)        {            Console.WriteLine(e.ChangeType + ":" + e.FullPath);        }    }}

3. Path 類提供了一組處理路徑的靜態方法

1)Path.GetDirectoryName(string path) 返回目錄名,需要注意路徑末尾是否有反斜線對結果是有影響的,如下:

Path.GetDirectoryName("d:\\abc") 將返回 d:\

Path.GetDirectoryName("d:\\abc\") 將返回 d:\abc

2)Path.GetRandomFileName()將返回隨機的檔案名稱

3)Path. GetFileNameWithoutExtension(“d:\\abc.txt”) 將返回abc

4)Path.GetInvalidPathChars() 將返回禁止在路徑中使用的字元

5)Path. GetInvalidFileNameChars()將返回禁止在檔案名稱中使用的字元

6)  Path.Combine(string left,string right)合并兩個路徑

需要注意的是,以上提到的這幾個檔案系統相關的類的底層都調用了windows的api,也就是說這些類只可以在windows系統下用,而在其他動作系統下是停用。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.