FileMode 列舉

定義

指定作業系統應該如何開啟檔案。Specifies how the operating system should open a file.

public enum class FileMode
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum FileMode
type FileMode = 
Public Enum FileMode
繼承
FileMode
屬性

欄位

Append 6

在檔案存在時開啟它並搜尋至檔案末端,或建立新檔案。Opens the file if it exists and seeks to the end of the file, or creates a new file. 這個需要 Append 使用權限。This requires Append permission. FileMode.Append 只能與 FileAccess.Write 一起使用。FileMode.Append can be used only in conjunction with FileAccess.Write. 嘗試搜尋到檔案結尾前的位置會擲回 IOException 例外狀況,而且任何讀取嘗試都會失敗並擲回 NotSupportedException 例外狀況。Trying to seek to a position before the end of the file throws an IOException exception, and any attempt to read fails and throws a NotSupportedException exception.

Create 2

指定作業系統應該建立新檔案。Specifies that the operating system should create a new file. 如果檔案已經存在,將覆寫此檔案。If the file already exists, it will be overwritten. 這個需要 Write 使用權限。This requires Write permission. FileMode.Create 等於要求檔案不存在時使用 CreateNew,否則使用 TruncateFileMode.Create is equivalent to requesting that if the file does not exist, use CreateNew; otherwise, use Truncate. 若檔案已經存在但為隱藏檔,則擲回 UnauthorizedAccessException 例外狀況。If the file already exists but is a hidden file, an UnauthorizedAccessException exception is thrown.

CreateNew 1

指定作業系統應該建立新檔案。Specifies that the operating system should create a new file. 這個需要 Write 使用權限。This requires Write permission. 如果檔案已經存在,則會擲回 IOException 例外狀況。If the file already exists, an IOException exception is thrown.

Open 3

指定作業系統應該開啟現有的檔案。Specifies that the operating system should open an existing file. 能否順利開啟檔案,取決於 FileAccess 列舉指定的值。The ability to open the file is dependent on the value specified by the FileAccess enumeration. 如果檔案不存在,就會擲回 FileNotFoundException 例外狀況。A FileNotFoundException exception is thrown if the file does not exist.

OpenOrCreate 4

指定作業系統,如果檔案存在應該開啟檔案,否則,應該建立新的檔案。Specifies that the operating system should open a file if it exists; otherwise, a new file should be created. 如果檔案是以 FileAccess.Read 開啟,則需要 Read 使用權限。If the file is opened with FileAccess.Read, Read permission is required. 如果檔案存取方式是 FileAccess.Write,則需要 Write 使用權限。If the file access is FileAccess.Write, Write permission is required. 如果檔案是以 FileAccess.ReadWrite 開啟,則需要 ReadWrite 使用權限。If the file is opened with FileAccess.ReadWrite, both Read and Write permissions are required.

Truncate 5

指定作業系統應該開啟現有的檔案。Specifies that the operating system should open an existing file. 檔案一旦開啟,應該截斷檔案使其大小為零個位元組。When the file is opened, it should be truncated so that its size is zero bytes. 這個需要 Write 使用權限。This requires Write permission. 嘗試讀取以 FileMode.Truncate 開啟的檔案會導致 ArgumentException 例外狀況。Attempts to read from a file opened with FileMode.Truncate cause an ArgumentException exception.

範例

下列FileStream的構造函式會開啟現有FileMode.Open的檔案 ()。The following FileStream constructor opens an existing file (FileMode.Open).

FileStream^ s2 = gcnew FileStream( name, FileMode::Open, FileAccess::Read, FileShare::Read );
FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);
Dim s2 As New FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read)

備註

如需建立檔案以及將文字寫入檔案的範例, 請參閱如何:將文字寫入檔案For an example of creating a file and writing text to a file, see How to: Write Text to a File. 如需從檔案讀取文字的範例, 請參閱如何:從檔案讀取文字For an example of reading text from a file, see How to: Read Text from a File. 如需讀取和寫入二進位檔案的範例, 請參閱如何:讀取和寫入新建立的資料檔案For an example of reading from and writing to a binary file, see How to: Read and Write to a Newly Created Data File.

FileStream IsolatedStorageFileStream在和的FileInfo方法中, 會在、和的許多函式中指定參數,以控制檔案的開啟方式。FileMode Open FileA FileMode parameter is specified in many of the constructors for FileStream, IsolatedStorageFileStream, and in the Open methods of File and FileInfo to control how a file is opened.

FileMode參數可控制是否覆寫、建立或開啟檔案, 或檔案的某種組合。FileMode parameters control whether a file is overwritten, created, opened, or some combination thereof. 使用Open來開啟現有的檔案。Use Open to open an existing file. 若要附加至檔案, 請Append使用。To append to a file, use Append. 若要截斷檔案或建立檔案 (如果檔案不存在), Create請使用。To truncate a file or create a file if it doesn't exist, use Create.

適用於

另請參閱