標籤:reader filter return object int data- 建立 inf 操作
檔案流
- Stream 類
- 讀寫類
- 其他系
FileStream fs = new FileStream(@"c:\t\c", FileMode.Create);
StreamWriter writer = new StreamWriter(fs);
檔案及檔案夾管理
- 對檔案和檔案夾操作的類
FileInfo 具體的檔案
File 提供static方法
DirectoryInfo 具體的檔案夾
Directory 提供static方法
FileSystemInfo
Path 檔案路徑類(路徑拆分和尋找)
顯示檔案及檔案夾的資訊
執行個體 - 搜尋資料夾下的檔案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# 檔案流