.Net筆記:System.IO之windows文件操作的深入分析
更新時(shí)間:2013年05月18日 16:31:35 作者:
本篇文章是對(duì).Net中windows文件操作的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
在.Net中處理系統(tǒng)文件相關(guān)的幾個(gè)類分別是File、Directory、FileInfo、DirectoryInfo、DriveInfo、FileSystemWatcher。本文介紹下這幾個(gè)類的用法。
1.File類提供靜態(tài)方法用來創(chuàng)建、移動(dòng)、復(fù)制、刪除文件的操作,并可以打開文件流
2.Directory類提供靜態(tài)方法用來創(chuàng)建、移動(dòng)、復(fù)制、刪除目錄的操作
3.FileInfo類用類實(shí)例實(shí)現(xiàn)創(chuàng)建、復(fù)制、移動(dòng)、刪除文件的操作
4.DirectoryInfo提供創(chuàng)建、移動(dòng)、復(fù)制、刪除目錄的操作,并可以枚舉子目錄
5.DriveInfo可以獲得windows操作系統(tǒng)中的磁盤信息
6.FileSystemWatcher用來監(jiān)視文件或目錄變化,并引發(fā)事件
7.Path類提供文件名目錄名操作的靜態(tài)方法
File、FileInfo、Directory、DirectoryInfo這幾個(gè)類的使用方法都非常簡(jiǎn)單就不做贅述了。
1.如何使用DriveInfo獲得windows系統(tǒng)磁盤信息
不允許在程序中自己構(gòu)造DriveInfo的實(shí)例,可以通過DriveInfo的靜態(tài)方法GetDrives()獲得windows系統(tǒng)中所有的磁盤,包括硬盤,cd以及u盤;注意在訪問磁盤屬性時(shí)需要先判斷其IsReady屬性是否為true,IsReady為false時(shí)訪問磁盤的一些屬性時(shí)會(huì)拋出異常。如下實(shí)例代碼:
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} 卷標(biāo):{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監(jiān)視目錄
FileSystemWatcher用來監(jiān)視目錄或者文件的修改,創(chuàng)建,刪除,要使FileSystemWatcher開始監(jiān)視必須設(shè)置其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)
{
//聲明要監(jiān)視的目錄
string watchPath = "D:\\watch";
FileSystemWatcher watcher = new FileSystemWatcher(watchPath, "*.*");
//添加文件變化處理事件
watcher.Changed += new FileSystemEventHandler(Watcher_Changed);
//添加文件創(chuàng)建處理事件
watcher.Created += new FileSystemEventHandler(Watcher_Created);
//添加文件刪除處理事件
watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);
//添加錯(cuò)誤處理
watcher.Error += new ErrorEventHandler(Watcher_Error);
//啟動(dòng)監(jiān)視
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("錯(cuò)誤:" + 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 類提供了一組處理路徑的靜態(tài)方法
1)Path.GetDirectoryName(string path) 返回目錄名,需要注意路徑末尾是否有反斜杠對(duì)結(jié)果是有影響的,如下:
Path.GetDirectoryName("d:\\abc") 將返回 d:\
Path.GetDirectoryName("d:\\abc\") 將返回 d:\abc
2)Path.GetRandomFileName()將返回隨機(jī)的文件名
3)Path. GetFileNameWithoutExtension(“d:\\abc.txt”) 將返回abc
4)Path.GetInvalidPathChars() 將返回禁止在路徑中使用的字符
5)Path. GetInvalidFileNameChars()將返回禁止在文件名中使用的字符
6) Path.Combine(string left,string right)合并兩個(gè)路徑
需要注意的是,以上提到的這幾個(gè)文件系統(tǒng)相關(guān)的類的底層都調(diào)用了windows的api,也就是說這些類只可以在windows系統(tǒng)下用,而在其他操作系統(tǒng)下是不可用的。
1.File類提供靜態(tài)方法用來創(chuàng)建、移動(dòng)、復(fù)制、刪除文件的操作,并可以打開文件流
2.Directory類提供靜態(tài)方法用來創(chuàng)建、移動(dòng)、復(fù)制、刪除目錄的操作
3.FileInfo類用類實(shí)例實(shí)現(xiàn)創(chuàng)建、復(fù)制、移動(dòng)、刪除文件的操作
4.DirectoryInfo提供創(chuàng)建、移動(dòng)、復(fù)制、刪除目錄的操作,并可以枚舉子目錄
5.DriveInfo可以獲得windows操作系統(tǒng)中的磁盤信息
6.FileSystemWatcher用來監(jiān)視文件或目錄變化,并引發(fā)事件
7.Path類提供文件名目錄名操作的靜態(tài)方法
File、FileInfo、Directory、DirectoryInfo這幾個(gè)類的使用方法都非常簡(jiǎn)單就不做贅述了。
1.如何使用DriveInfo獲得windows系統(tǒng)磁盤信息
不允許在程序中自己構(gòu)造DriveInfo的實(shí)例,可以通過DriveInfo的靜態(tài)方法GetDrives()獲得windows系統(tǒng)中所有的磁盤,包括硬盤,cd以及u盤;注意在訪問磁盤屬性時(shí)需要先判斷其IsReady屬性是否為true,IsReady為false時(shí)訪問磁盤的一些屬性時(shí)會(huì)拋出異常。如下實(shí)例代碼:
復(fù)制代碼 代碼如下:
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} 卷標(biāo):{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監(jiān)視目錄
FileSystemWatcher用來監(jiān)視目錄或者文件的修改,創(chuàng)建,刪除,要使FileSystemWatcher開始監(jiān)視必須設(shè)置其EnableRaisingEvents屬性為true,如下示例:
復(fù)制代碼 代碼如下:
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)
{
//聲明要監(jiān)視的目錄
string watchPath = "D:\\watch";
FileSystemWatcher watcher = new FileSystemWatcher(watchPath, "*.*");
//添加文件變化處理事件
watcher.Changed += new FileSystemEventHandler(Watcher_Changed);
//添加文件創(chuàng)建處理事件
watcher.Created += new FileSystemEventHandler(Watcher_Created);
//添加文件刪除處理事件
watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);
//添加錯(cuò)誤處理
watcher.Error += new ErrorEventHandler(Watcher_Error);
//啟動(dòng)監(jiān)視
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("錯(cuò)誤:" + 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 類提供了一組處理路徑的靜態(tài)方法
1)Path.GetDirectoryName(string path) 返回目錄名,需要注意路徑末尾是否有反斜杠對(duì)結(jié)果是有影響的,如下:
Path.GetDirectoryName("d:\\abc") 將返回 d:\
Path.GetDirectoryName("d:\\abc\") 將返回 d:\abc
2)Path.GetRandomFileName()將返回隨機(jī)的文件名
3)Path. GetFileNameWithoutExtension(“d:\\abc.txt”) 將返回abc
4)Path.GetInvalidPathChars() 將返回禁止在路徑中使用的字符
5)Path. GetInvalidFileNameChars()將返回禁止在文件名中使用的字符
6) Path.Combine(string left,string right)合并兩個(gè)路徑
需要注意的是,以上提到的這幾個(gè)文件系統(tǒng)相關(guān)的類的底層都調(diào)用了windows的api,也就是說這些類只可以在windows系統(tǒng)下用,而在其他操作系統(tǒng)下是不可用的。
相關(guān)文章
MVC+EasyUI+三層新聞網(wǎng)站建立 后臺(tái)登錄界面的搭建(二)
這篇文章主要為大家詳細(xì)介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第二篇,教大家如何搭建后臺(tái)登錄界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Asp.net使用HttpModule壓縮并刪除空白Html請(qǐng)求的實(shí)現(xiàn)代碼
當(dāng)我們壓縮我的Response后再傳到Client端時(shí),可以明顯節(jié)省寬帶. 提升Site的性能. 現(xiàn)在的瀏覽器大部分都支持Gzip,Deflate壓縮2011-11-11
Asp.net?core中依賴注入的實(shí)現(xiàn)
這篇文章介紹了Asp.net?core中依賴注入的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車
這篇文章介紹了ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
.net使用jwt進(jìn)行身份認(rèn)證的流程記錄
這篇文章主要給大家介紹了關(guān)于.net使用jwt進(jìn)行身份認(rèn)證的相關(guān)資料,JWT是Auth0提出的通過對(duì)JSON進(jìn)行加密簽名來實(shí)現(xiàn)授權(quán)驗(yàn)證的方案,本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
MAUI項(xiàng)目中使用SnackBar與Toast通知功能
這篇文章介紹了MAUI項(xiàng)目中使用SnackBar與Toast通知功能的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02

