共用方式為


FileSystem.Lock 方法 (Int32)

控制項由其他處理序存取以 Open 函式開啟的整個或部分檔案。My 功能提供比 LockUnlock 更強的檔案 I/O 作業產能和效能。如需詳細資訊,請參閱 My.Computer.FileSystem 物件

命名空間: Microsoft.VisualBasic
組件: Microsoft.VisualBasic (在 microsoft.visualbasic.dll 中)

語法

'宣告
Public Shared Sub Lock ( _
    FileNumber As Integer _
)
'用途
Dim FileNumber As Integer

FileSystem.Lock(FileNumber)
public static void Lock (
    int FileNumber
)
public:
static void Lock (
    int FileNumber
)
public static void Lock (
    int FileNumber
)
public static function Lock (
    FileNumber : int
)

參數

  • FileNumber
    必要項。任何有效的檔案號碼。

備註

如需詳細資訊,請參閱 Visual Basic 的主題 Lock、Unlock 函式

LockUnlock 函式用於有數個處理序可能需要存取相同檔案的環境中。

LockUnlock 函式永遠要成對使用。LockUnlock 的引數必須完全相符。

如果未提供 Record 或 FromRecord 和 ToRecord,將會鎖定整個檔案。如果只指定 Record,將鎖定/解除鎖定單一資料錄。

如果已經開啟檔案供循序輸入或輸出使用,則不論 FromRecord 和 **ToRecord 指定的範圍為何,LockUnlock 都會影響整個檔案。

範例

這個範例說明 LockUnlock 函式的用法。這個範例假設 People.txt 是一個包含 Person 結構資料錄的檔案。

Structure Person
   Dim Name As String
   Dim ID As Integer
End Structure

Sub PutInLockedFile(ByVal index As Integer, ByVal onePerson As Person)
   Try
      FileOpen(1, "c:\people.txt", OpenMode.Binary)
      Lock(1)
      FilePut(index, onePerson)
      Unlock(1)
      FileClose(1)
   Catch
      ' Error recovery code here.
   End Try
End Sub

平台

Windows 98、 Windows 2000 SP4、 Windows Millennium Edition、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition

.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。

版本資訊

.NET Framework

支援版本:2.0、1.1、1.0

請參閱

參考

FileSystem 類別
FileSystem 成員
Microsoft.VisualBasic 命名空間
IOException Class

其他資源

Lock、Unlock 函式
FileOpen 函式
使用 Visual Basic 存取檔案