1
A
回答
2
MS對如何檢查是否可以訪問文件或者不是你嘗試使用他們編寫的名爲FileLocked函數打開它之前一個很好的例子。
Sub YourMacro()
Dim strFileName As String
' Full path and name of file.
strFileName = "C:\test.doc"
' Call function to test file lock.
If Not FileLocked(strFileName) Then
' If the function returns False, open the document.
Documents.Open strFileName
End If
End Sub
這裏是功能(如寫MS):從微軟
Function FileLocked(strFileName As String) As Boolean
On Error Resume Next
' If the file is already opened by another process,
' and the specified type of access is not allowed,
' the Open operation fails and an error occurs.
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1
' If an error occurs, the document is currently open.
If Err.Number <> 0 Then
' Display the error number and description.
MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description
FileLocked = True
Err.Clear
End If
End Function
0
Followingon從Issun的答案,如果你發現,微軟的FileLocked功能不工作(例如,我發現它會錯誤地聲稱當前正在錄製的WTV文件沒有被鎖定),那麼你可以使用這種方法,而不是更加殘忍:
Function FileLocked(sFilename) As Boolean
Dim oFile, sNewFile, iCount
If fso.FileExists(sFilename) = False Then
FileLocked = False
Exit Function
End If
' Pick a random, unused, temp name
iCount = 0
Do
sNewFile = sFilename & ".tmp" & iCount
iCount = iCount + 1
Loop Until fso.FileExists(sNewFile) = False
' Try to rename it to that. If it fails, then it is in use
On Error Resume Next
fso.MoveFile sFilename, sNewFile
If Err.Number = 0 Then
' It moved okay, so rename it back
fso.MoveFile sNewFile, sFilename
FileLocked = False
Else
FileLocked = True
End If
On Error Goto 0
End Function
+0
謝謝你們。 – LianxiWang
相關問題
- 1. 確定該文件沒有被任何其他進程使用
- 2. 如何檢查文件是否被其他進程鎖定/使用NSIS
- 3. 如何確定其他進程是否正在使用文件描述符?
- 4. 如何確定文件是否正在被另一進程使用(Java)
- 5. 確定文件(dll/exe)是否被進程或庫鎖定
- 6. 在Python中檢查文件是否未打開(未被其他進程使用)
- 7. 如何檢查文件是否被其他應用程序使用?
- 8. 如何檢測文件是否被窗口中的其他進程打開
- 9. 如何檢查文件是否被其他進程訪問並釋放它?
- 10. c#文件移動(文件已被其他進程使用)
- 11. 如何確定CALayer是否完全被其他CALayer覆蓋?
- 12. 如何,而文件是否被其它進程
- 13. 如何檢查文件是否被其他進程或應用程序鎖定(windows)
- 14. 如何判斷相機是否正在被其他進程使用?
- 15. 如何確定Java中是否有其他進程或可執行文件是32位還是64位
- 16. 如何確定控件是否爲其他控件的容器
- 17. 寫入文件時出錯 - 它被其他進程使用
- 18. 無法訪問已被其他進程使用的文件
- 19. 文件正在被其他進程使用
- 20. 如何判斷文件是否被其他應用程序打開?
- 21. 如何在Android上檢查文件是否被其他應用程序
- 22. 如何使用FileSystemWatcher檢查文件是否被另一個進程鎖定?
- 23. 如何在下載之前檢查一個zip文件是否未被其他進程使用
- 24. 如何確定是否在用戶界面進程的進程?
- 25. 如何檢查一個表數據是否在其他進程中被引用
- 26. c#如果該文件被其他進程處理,如何訪問文件?
- 27. 檢查文件是否被進程文件句柄鎖定
- 28. 確定文件是否已被選中
- 29. 如何確定進程句柄是否是當前進程?
- 30. 如何確定Console.Out是否已被重定向到文件?
+1我已經多次部署這種技術來檢查共享網絡上的文件是否已被使用(儘管我唯一擔心的是該文件已經在Excel中打開) – brettdj
謝謝你們。 – LianxiWang
如果這個或其他答案有效,您可以通過選擇左上方的箭頭來「接受」它作爲答案。謝謝! – aevanko