在VB6中,如何判断某个文件(如test.txt)正在被其他程序控制?因为这个被控制的文件test.txt我在VB6中无法用open打开,需判断控制被解除后我才能用open语句打开它。

解决方案 »

  1.   

    可以这样判断文件是否已经打开:Function IsOpen(sFile As String) As Boolean
         Dim fFile As Integer
         IsOpen = False
         fFile = FreeFile()
         On Error GoTo ErrOpen
         Open sFile For Binary Lock Read Write As fFile
         Close fFile
         Exit Function
    ErrOpen:
         If Err.Number <> 70 Then
            Msg = "Error # " & Str(Err.Number) & " was generated by " _
            & Err.Source & Chr(13) & Err.Description
            MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
         Else
            IsOpen = True
         End If
    End Function