2010-03-31 7 views
3

서버에 파일 위치가 있습니다.다른 사용자가 엑셀 파일을 열려 있는지 확인하십시오.

VB.NET에서 작성한 응용 프로그램을 사용하여 읽기 전용 모드로 파일을 엽니 다.

사용자 1 읽기 전용 모드로 파일을 엽니 다. 사용자 2가 파일이 열려 있거나 상태가 아닌 것을 어떻게 감지 할 수 있습니까?

감사합니다,

답변

1

파일이 읽기 전용 상태 인 경우 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

최적으로 들리지는 않지만 사실입니다. 자물쇠가 잠겨 있는지를 감지 할 수있는 방법이 있더라도 자물쇠 확인과 실제로 파일을 여는 누군가가 파일을 열 수 있으므로 최선의 방법은 파일을 열어 예외를 잡는 것입니다. –

관련 문제