C# 檔案流

來源:互聯網
上載者:User

標籤:reader   filter   return   object   int   data-   建立   inf   操作   

檔案流
  • Stream 類
    • 按存取位置分
      • FileStream, MemoryStream, BufferedStream

  • 讀寫類
    • BinaryReader和BinaryWriter

    • TextReader和TextWriter

      • StreamReader和StreamWriter

      • StringReader和StringWriter

  • 其他系
    • FileStream fs = new FileStream(@"c:\t\c", FileMode.Create);

    • StreamWriter writer = new StreamWriter(fs);

檔案及檔案夾管理
  • 對檔案和檔案夾操作的類
    • FileInfo 具體的檔案

    • File 提供static方法

    • DirectoryInfo 具體的檔案夾

    • Directory 提供static方法

    • FileSystemInfo

      • 是FileInfo及DirectoryInfo的父類

    • Path 檔案路徑類(路徑拆分和尋找)

顯示檔案及檔案夾的資訊
  • 建立對象
    • new FileInfo(檔案無聊路徑)

  • 常用屬性

 

執行個體 - 搜尋資料夾下的檔案ListAllFiles.cs
class Program{        static void Main(string[] args) {            ListFiles(new DirectoryInfo(@"F:\download"));        }        public static void ListFiles(FileSystemInfo info) {            if (!info.Exists) return;            //info當成一個為DirectoryInfo,如果不成功則當成null            DirectoryInfo dir = info as DirectoryInfo;            if (dir == null) return;  //不是目錄            FileSystemInfo[] files = dir.GetFileSystemInfos();            for (int i = 0; i < files.Length; i++) {                FileInfo file = files[i] as FileInfo;                if (file != null) {  //是檔案                    Console.WriteLine(                        file.FullName + "\t" + file.Length);                } else {                    ListFiles(files[i]);  //對於子目錄,進行遞迴調用                }            }        }}
 執行個體2:檔案監視watcher.cs
    class Program    {        static void Main(string[] args) {         const string path = @"G:\Dev-Cpp\代碼大全\OS實驗";            FileSystemWatcher watcher = new FileSystemWatcher();            watcher.Path = path;            watcher.Filter = "*.cpp";            watcher.NotifyFilter = NotifyFilters.LastAccess |                NotifyFilters.LastWrite | NotifyFilters.FileName |                NotifyFilters.DirectoryName;            //事件處理函數            watcher.Changed += new FileSystemEventHandler(onChanged);            watcher.Created += new FileSystemEventHandler(onChanged);            watcher.Deleted += new FileSystemEventHandler(onChanged);            watcher.Renamed += new RenamedEventHandler(OnRenamed);            //開啟監視            watcher.EnableRaisingEvents = true;                        //等待使用者輸入q才結束程式            Console.WriteLine("Press ‘q‘ to quit the sample.");            while (Console.Read() != ‘q‘) ;        }        //事件處理函數        public static void onChanged(object source, FileSystemEventArgs e) {            //顯示哪些檔案做了哪些修改            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);        }        public static void OnRenamed(object source, RenamedEventArgs e) {            //顯示被更改的檔案名稱            Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);        }    }

//在目錄下建立一個檔案即能在程式顯示對 .cpp檔案進行的操作類型

 

C# 檔案流

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.