[讀書筆記] Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 第三十章

  • 1290
  • 0

閱讀Stephens' C#教材第三十章筆記

 

Chapter 30 Using File System Classes.
 
本章討論檔案讀寫之外的其他操作,例如重新命名、移動、刪除檔案或刪除目錄,或是讀寫整個檔案不必使用StreamReader。
 
開始之前還是提醒一下System.IO命名空間匯入不可少。
 
本章主要介紹類別有:DriverInfo, DirectoryInfo, Directory, FileInfo, File, Path六種。
 
DriverInfo類別提供許多關於系統磁碟的資訊,它的靜態方法GetDrives會傳回一個陣列,裡面包含所有的系統磁碟。
中提到常用的幾種屬性例如:AvailableFreeSpace, DriverFormat, DriveType, IsReady, Name, RootDirectory, TotalFreeSpace, TotalSize, VolumeLabel等,詳細的內容請看微軟網頁
 
ListDrives程式示範使用DriverInfo類別取得磁碟資訊的運用,可以看見磁區總容量,可用空間,磁碟的格式(NTFS, FAT32, ...), 磁區的型態(Fixed, CDRom,Removeable, Network, ...)
 
 
DirectoryInfo類別提供許多關於檔案目錄的資訊,書中提到常用的幾種方法為:Create, CreateSubdirectory, Delete, GetDirectories, GetFiles, MoveTo,屬性則有:Attributes, CreationTime, Exists, FullName, LastAccessTime, LastWriteTime, Name, Parent, Root等,詳細的內容請看微軟網頁
 
UseDirectoryInfo程式示範使用DirectoryInfo類別取得目錄資訊的情形。(註:Wrox下載的C#範例程式中並沒有這個程式,但是在Stephens的vb 24-hour Trainer中第32章有這支程式,一樣可供參考,下載連結在此)
 
Directory類別提供許多操作目錄的靜態方法,這是比使用DirectoryInfo類別方便之處,因為Directory類別方法可以直接使用,不用創建實例,書中提到常用的幾種方法為:CreateDirectory, Delete, Exists, GetCreationTime, GetDirectories, GetDirectoryRoot, GetFiles, GetLastAccessTime, gETlastWriteTime, GetParent, Move, SetCreationTime, SetLastaCCESStIME, SetLastWriteTime等,詳細的內容請看微軟網頁
 
FileInfo類別提供許多關於檔案的資訊,書中提到常用的幾種方法為:CopyTo, Decrypt, Delete, Encrypt, MoveTo,屬性則有:Attributes, CreationTimes, Directory, Exists, Extension, FullName, IsReadOnly, LastAccessTime, LastWriteTime, Length, Name等,詳細的內容請看微軟網頁
 
UseFileInfo程式示範使用FileInfo類別取得檔案資訊的情形。
 
File類別提供許多操作檔案的靜態方法,這是比使用FileInfo類別方便之處,同樣地也是因為File類別方法可以直接使用,不用創建實例,書中提到常用的幾種方法為:AppendAllText, Copy, Create, Decypt, Delete, Encypt, Exists, GetAttributes, GetCreationTime, GetLastAccessTime, GetLastWriteTime, Move, ReadAllBytes, ReadAllLines, ReadAllText, SetAttributes, SetCreationTime, SetLastAccessTime, SetLastWriteTime, WriteAllBytes, WriteAllInes, WriteAllText等,詳細的內容請看微軟網頁
 
Path類別提供許多操作檔案路徑名稱的靜態方法,例如使用其ChangeExtension用來改變檔案的副檔名。書中提到常用的幾種方法為: ChangeExtension, Combine, GetDirectoryName, GetExtension, GetFileName, GetFileNameWithoutExtension, GetTempFileName, GetTempPath等,詳細的內容請看微軟網頁
 
TRY IT中以SearchForFiles程式示範使用DirectoryInfo及FileInfo類別,進行檔案名稱的搜尋。