校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > .Net筆記:System.IO之windows文件操作的深入分析

.Net筆記:System.IO之windows文件操作的深入分析

熱門標簽:長春呼叫中心外呼系統哪家好 智能電話營銷外呼系統 凱立德導航官網地圖標注 電銷語音自動機器人 鄭州400電話辦理 聯通 五常地圖標注 地圖標注和認領 萊蕪外呼電銷機器人價格 戶外地圖標注軟件手機哪個好用
在.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系統下用,而在其他操作系統下是不可用的。

標簽:湖州 宣城 福州 紅河 岳陽 西寧 衢州 西藏

巨人網絡通訊聲明:本文標題《.Net筆記:System.IO之windows文件操作的深入分析》,本文關鍵詞  .Net,筆記,System.IO,之,windows,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《.Net筆記:System.IO之windows文件操作的深入分析》相關的同類信息!
  • 本頁收集關于.Net筆記:System.IO之windows文件操作的深入分析的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 松江区| 香河县| 宜阳县| 澄迈县| 进贤县| 耿马| 吴旗县| 环江| 永州市| 崇信县| 平武县| 开化县| 义乌市| 萨迦县| 太白县| 嘉定区| 梅河口市| 拉萨市| 方正县| 涿鹿县| 舞阳县| 五大连池市| 黔南| 泸水县| 竹溪县| 永靖县| 克什克腾旗| 格尔木市| 福海县| 石棉县| 色达县| 砚山县| 锡林郭勒盟| 虹口区| 临漳县| 义马市| 嘉荫县| 平乐县| 海口市| 车险| 司法|