0
我一直在嘗試如何知道哪些訪問我的excel文件。 在某些情況下,例如,如果您打開一個word文件「winword.doc」 它將創建〜$ winword.doc文件,其中如果您在記事本中打開它將顯示當前用戶。然而這並不適用於所有人。如何知道用戶訪問我的excel文件在vb.net
我想知道如何在vb.net或vb代碼中檢查excel文件的用戶。
Private Function CheckFile(ByVal filename As String) As Boolean
Try
'Check file access if can be opened
System.IO.File.Open(filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None).Close()
'Return CheckFile to False if File is not opened
Return False
Catch ex As Exception
'If file deemed to be open Boolean is True
Return True
End Try
End Function
Private Function CheckIfRunning(ByVal processname As String) As Boolean
'processname = "SLDWORKS"
Dim CurrentSessionID As Integer = Process.GetCurrentProcess.SessionId
Dim val As String = "no"
For Each proc As Process In Process.GetProcesses
If proc.SessionId = CurrentSessionID Then
val = "yes"
End If
Next
If val = "no" Then
Return False
Else
Return True
End If
End Function
Public Sub Main()
Dim freader As System.IO.StreamReader
'Loop while file still opened
Do While CheckFile("location.xls") = True
If CheckIfRunning("EXCEL.EXE") = True Then
Else
MsgBox("file is not running in process. File Must be opened in another location")
End If
Threading.Thread.Sleep(5000)
Loop
End Sub
希望你能幫助我。我想知道誰訪問我的文件以及誰鎖定了它。 這有一個缺陷,如果你在你的站打開Excel文件,它將始終返回false。