2010-03-31 113 views
3

我有一個excel文件放在服務器上。檢查excel文件是否被其他用戶打開

我正在使用VB.NET編寫的應用程序以只讀模式打開文件。

用戶1以只讀模式打開文件。 用戶2如何檢測該文件是否處於打開狀態?

感謝, 的

回答

1

您需要測試文件只讀,因爲如果文件爲ReadOnly狀態,ReadWrite將失敗。 下面是一些方法。不知道除了Randy Birch的最後一張之外,我從哪裏得到了他們。 速度測試有利於FileIsOpen3和FileIsOpen4。

Function FileIsOpen1(ByVal pathfile As String) As Boolean 
    Dim ff As Integer 
    If System.IO.File.Exists(pathfile) Then 
     Try 
      ff = FreeFile() 
      Microsoft.VisualBasic.FileOpen(ff, pathfile, OpenMode.Binary, OpenAccess.Read, OpenShare.LockReadWrite) 
      Return False 
     Catch 
      Return True 
     Finally 
      FileClose(ff) 
     End Try 
     Return True 
    End If 
End Function 

Function FileIsOpen2(ByVal pathfile As String) As Boolean 
    Dim stream As FileStream = Nothing 
    Dim fi As FileInfo = Nothing 
    If System.IO.File.Exists(pathfile) Then 
     Try 
      fi = New System.IO.FileInfo(pathfile) 
      stream = fi.Open(FileMode.Open, FileAccess.Read, FileShare.None) 
      Return True 
     Catch generatedExceptionName As IOException 
      Return False 
     Finally 
      If stream IsNot Nothing Then 
       stream.Close() 
      End If 
      fi = Nothing 
     End Try 
     Return True 
    End If 
End Function 

Private Function FileIsOpen3(ByVal pathfile As String) As Boolean 
    Try 
     Dim fs As IO.FileStream = IO.File.Open(pathfile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None) 
     fs.Close() 
     fs.Dispose() 
     fs = Nothing 
     Return False 
    Catch ex As IO.IOException ' File open 
     Return True 
    Catch ex As Exception ' Unknown error 
     Return True 
    End Try 
End Function 

Private Declare Function CreateFile Lib "kernel32" _ 
    Alias "CreateFileA" _ 
    (ByVal lpFileName As String, _ 
    ByVal dwDesiredAccess As Long, _ 
    ByVal dwShareMode As Long, _ 
    ByVal lpSecurityAttributes As Long, _ 
    ByVal dwCreationDisposition As Long, _ 
    ByVal dwFlagsAndAttributes As Long, _ 
    ByVal hTemplateFile As Long) As Long 
Private Declare Function CloseHandle Lib "kernel32" _ 
    (ByVal hFile As Long) As Long 

' Method 
Shared Function FileIsOpen4(ByVal pathfile As String) As Boolean 
    ' Is File In Use ©1996-2009 Randy Birch 
    ' http://vbnet.mvps.org/index.html?code/fileapi/createfile_inuse.htm 
    Const GENERIC_READ As Long = &H80000000 
    Const INVALID_HANDLE_VALUE As Long = -1 
    Const OPEN_EXISTING As Long = 3 
    Const FILE_ATTRIBUTE_NORMAL As Long = &H80 
    Dim hFile As Long 

    If System.IO.File.Exists(pathfile) Then 
     Try 
      ' note that FILE_ATTRIBUTE_NORMAL (&H80) has a different value than VB's constant vbNormal (0)! 
      hFile = CreateFile(pathfile, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&) 
      ' this will evaluate to either -1 (File in use) or 0 (File free) 
      Return hFile = INVALID_HANDLE_VALUE 
     Catch ex As Exception 
      MessageBox.Show(ex.ToString) 
     Finally 
      CloseHandle(hFile) 
     End Try 
    Else 
     Return True 
    End If 
End Function 
1

第二用戶可以嘗試打開該文件在讀寫模式下知道如果正在使用的文件。

+2

聽起來並不理想,但它是真實的。即使你有一些方法來檢測鎖是否有可能在你的鎖定檢查和實際打開文件之間有人可以打開文件,所以最好的辦法就是嘗試打開文件並捕獲異常(如果有的話)。 – 2010-03-31 13:13:47

相關問題