Share via


FileOpen 函式

更新:2007 年 11 月

開啟輸入或輸出用的檔案。

My 功能在檔案 I/O 作業中的產能和效能勝過 FileOpen。如需詳細資訊,請參閱 My.Computer.FileSystem 物件

Public Sub FileOpen( _
   ByVal FileNumber As Integer, _
   ByVal FileName As String, _
   ByVal Mode As OpenMode, _
   Optional ByVal Access As OpenAccess = OpenAccess.Default, _
   Optional ByVal Share As OpenShare = OpenShare.Default, _
   Optional ByVal RecordLength As Integer = -1 _
)

參數

  • FileNumber
    必要項。任何有效的檔案號碼。使用 FreeFile 函式取得下一個可用的檔案號碼。

  • FileName
    必要項。指定檔案名稱的 String 運算式,可能包含目錄或資料夾和磁碟。

  • Mode
    必要項。指定檔案模式的列舉型別:Append、Binary、Input、Output 或 Random (如需詳細資訊,請參閱 OpenMode 列舉型別)。

  • Access
    選擇項。指定在開啟檔案上所允許之作業的列舉型別:Read、Write 或 ReadWrite。預設值為 ReadWrite (如需詳細資訊,請參閱 OpenAccess 列舉型別)。

  • Share
    選擇項。指定其他處理序不得在開啟檔案上執行之作業的列舉型別 (Enumeration):Shared、Lock Read、Lock Write 和 Lock Read Write預設值為 Lock Read Write (如需詳細資訊,請參閱 OpenShare 列舉型別)。

  • RecordLength
    選擇項。小於或等於 32,767 (位元組) 的數字。對於為隨機存取開啟的檔案而言,這個值是資料錄長度。對於循序檔而言,這個值是緩衝的字元數。

例外狀況

例外狀況類型

錯誤代碼

條件

ArgumentException

5

Access、Share 或 Mode 無效。

ArgumentException

5

WriteOnly 檔案是為 Input 而開啟。

ArgumentException

5

ReadOnly 檔案是為 Output 而開啟。

ArgumentException

5

ReadOnly 檔案是為 Append 而開啟。

ArgumentException

5

資料錄的長度為負數 (而且不等於 -1)。

IOException

52

FileNumber 無效 (<-1 或 >255),或是 FileNumber 已經在使用中。

IOException

55

FileName 已經開啟,或 FileName 無效。

如果將使用非結構化錯誤處理的 Visual Basic 6.0 應用程式升級,請參閱「錯誤代碼」資料行 (您可以將錯誤代碼與 Number 屬性 (Err 物件) 比對)。但是,請盡可能考慮以 Visual Basic 的結構化例外處理概觀 取代這類錯誤控制項。

備註

提供 FileOpen 函式以取得回溯相容性,但可能會影響效能。對於非舊版應用程式,My.Computer.FileSystem 物件會提供更好的效能。如需詳細資訊,請參閱使用 Visual Basic 存取檔案

您必須先開啟檔案,才能對其執行任何 I/O 作業。FileOpen 會將 I/O 的緩衝區配置給檔案,並決定要用於緩衝區的存取模式。

安全性注意事項:

在寫入檔案時,如果應用程式嘗試寫入的檔案不存在,則應用程式可能需要建立檔案。若要這樣做的話,應用程式需要檔案建立所在之目錄的使用權限。但是,如果 FileName 指定的檔案已存在,則應用程式需要檔案本身的 Write 使用權限。在可能的情況下,為協助安全性的提升,在部署期間建立檔案,且只授與該檔案的 Write 使用權限,而不要授與整個目錄的使用權限。為了增強安全性,請將資料寫入使用者目錄,而不要寫入根目錄或 [Program Files] 目錄。

使用 FreeFile() 函式可以找出要開啟的通道。

安全性注意事項:

FileOpen 函式需要 FileIOPermissionAccess 列舉型別中的 Read 存取,而這可能會在部分信任的情況下影響其執行。如需詳細資訊,請參閱 FileIOPermissionAccess要求使用權限

範例

這個範例會說明使用 FileOpen 函式,對檔案輸入和輸出的各種方法。

下列程式碼會以 Input 模式開啟 TestFile 檔案。

FileOpen(1, "TESTFILE", OpenMode.Input)
' Close before reopening in another mode.
FileClose(1)

這個範例會以 Binary 模式來開啟只允許寫入作業的檔案。

FileOpen(1, "TESTFILE", OpenMode.Binary,OpenAccess.Write)
' Close before reopening in another mode.
FileClose(1)

下列範例以 Random 模式開啟檔案。此檔案包含 Person 結構的資料錄。

Structure Person
    <VBFixedString(30)> Dim Name As String
    Dim ID As Integer
End Structure
Public Sub ExampleMethod()
    ' Count 30 for the string, plus 4 for the integer.
    FileOpen(1, "TESTFILE", OpenMode.Random, , , 34)
    ' Close before reopening in another mode.
    FileClose(1)
End Sub

這個程式碼範例會以 Output 模式來開啟檔案,任何的處理序都可以讀取或寫入這個檔案。

FileOpen(1, "TESTFILE", OpenMode.Output, OpenAccess.Default, OpenShare.Shared)
' Close before reopening in another mode.
FileClose(1)

這個程式碼範例會以 Binary 模式來開啟讀取用的檔案,其他的處理序無法讀取這個檔案。

FileOpen(1, "TESTFILE", OpenMode.Binary, OpenAccess.Read, _
   OpenShare.LockRead)

智慧型裝置開發人員注意事項

不支援這個函式。

需求

命名空間Microsoft.VisualBasic

**模組︰**FileSystem

組件:Visual Basic Runtime Library (在 Microsoft.VisualBasic.dll 中)

請參閱

參考

FileClose 函式

FreeFile 函式

其他資源

在 Visual Basic 中讀取檔案

在 Visual Basic 中寫入檔案