一般 I/O 工作Common I/O Tasks

System.IO 命名空間 (Namespace) 提供了幾個允許可在檔案、目錄和資料流上執行各種動作 (例如讀取和寫入) 的類別。The System.IO namespace provides several classes that allow for various actions, such as reading and writing, to be performed on files, directories, and streams. 如需詳細資訊,請參閱檔案和資料流 I/OFor more information, see File and Stream I/O.

一般檔案工作Common File Tasks

若要執行相關作業…To do this... 請參閱這個主題中的範例…See the example in this topic...
建立文字檔Create a text file File.CreateText 方法File.CreateText method

FileInfo.CreateText 方法FileInfo.CreateText method

File.Create 方法File.Create method

FileInfo.Create 方法FileInfo.Create method
寫入文字檔Write to a text file 如何:將文字寫入檔案How to: Write Text to a File

如何:寫入文字檔 (C++/CLI)How to: Write a Text File (C++/CLI)
從文字檔讀取Read from a text file 如何:讀取檔案中的文字How to: Read Text from a File
將文字附加至檔案Append text to a file 如何:開啟並附加至記錄檔How to: Open and Append to a Log File

File.AppendText 方法File.AppendText method

FileInfo.AppendText 方法FileInfo.AppendText method
重新命名檔案或移動檔案Rename or move a file File.Move 方法File.Move method

FileInfo.MoveTo 方法FileInfo.MoveTo method
刪除檔案Delete a file File.Delete 方法File.Delete method

FileInfo.Delete 方法FileInfo.Delete method
複製檔案Copy a file File.Copy 方法File.Copy method

FileInfo.CopyTo 方法FileInfo.CopyTo method
取得檔案大小Get the size of a file FileInfo.Length 屬性FileInfo.Length property
取得檔案的屬性Get the attributes of a file File.GetAttributes 方法File.GetAttributes method
設定檔案的屬性Set the attributes of a file File.SetAttributes 方法File.SetAttributes method
判斷檔案是否存在Determine whether a file exists File.Exists 方法File.Exists method
讀取二進位檔案Read from a binary file 如何:讀取和寫入新建立的資料檔案How to: Read and Write to a Newly Created Data File
寫入至二進位檔案Write to a binary file 如何:讀取和寫入新建立的資料檔案How to: Read and Write to a Newly Created Data File
擷取副檔名Retrieve a file name extension Path.GetExtension 方法Path.GetExtension method
擷取檔案的完整路徑Retrieve the fully qualified path of a file Path.GetFullPath 方法Path.GetFullPath method
從路徑擷取檔案名稱和副檔名Retrieve the file name and extension from a path Path.GetFileName 方法Path.GetFileName method
變更檔案的副檔名Change the extension of a file Path.ChangeExtension 方法Path.ChangeExtension method

一般目錄工作Common Directory Tasks

若要執行相關作業…To do this... 請參閱這個主題中的範例…See the example in this topic...
存取特殊資料夾中的檔案,例如 [我的文件]Access a file in a special folder such as My Documents 如何:將文字寫入檔案How to: Write Text to a File
建立目錄Create a directory Directory.CreateDirectory 方法Directory.CreateDirectory method

FileInfo.Directory 屬性FileInfo.Directory property
建立子目錄Create a subdirectory DirectoryInfo.CreateSubdirectory 方法DirectoryInfo.CreateSubdirectory method
重新命名目錄或移動目錄Rename or move a directory Directory.Move 方法Directory.Move method

DirectoryInfo.MoveTo 方法DirectoryInfo.MoveTo method
複製目錄Copy a directory 如何:複製目錄How to: Copy Directories
刪除目錄Delete a directory Directory.Delete 方法Directory.Delete method

DirectoryInfo.Delete 方法DirectoryInfo.Delete method
請參閱目錄中的檔案與子目錄See the files and subdirectories in a directory 如何:列舉目錄和檔案How to: Enumerate Directories and Files
尋找目錄的大小Find the size of a directory System.IO.Directory 類別System.IO.Directory class
判斷目錄是否存在Determine whether a directory exists Directory.Exists 方法Directory.Exists method

另請參閱See also