判斷檔案是否有被其它程式Lock的方式
之前呼叫檔案印表到adobePDF,讓它產生pdf檔時,為了判斷檔案是否還有被adobePDF Call所以要檢查檔案是否有被它Lock。
目前使用暴力法,對那個檔案開啟來寫,如果有產生錯誤的話,表示檔案有被其他程式使用。
01 Public Shared Function IsFileInUse(ByVal sFileName As String) As Boolean
02 ' If the file is already opened by another process and the specified type of access
03 ' is not allowed the Open operation fails and an error occurs.
04 Dim nFileNum As Integer
05
06 nFileNum = FreeFile()
07
08 Try
09 FileOpen(nFileNum, sFileName, OpenMode.Binary, OpenAccess.Write)
10 FileClose(nFileNum)
11 If FileLen(sFileName) > 0 Then
12 Return False
13 Else
14 Return True
15 End If
16
17 Catch
18 Return True
19 End Try
20 End Function
02 ' If the file is already opened by another process and the specified type of access
03 ' is not allowed the Open operation fails and an error occurs.
04 Dim nFileNum As Integer
05
06 nFileNum = FreeFile()
07
08 Try
09 FileOpen(nFileNum, sFileName, OpenMode.Binary, OpenAccess.Write)
10 FileClose(nFileNum)
11 If FileLen(sFileName) > 0 Then
12 Return False
13 Else
14 Return True
15 End If
16
17 Catch
18 Return True
19 End Try
20 End Function
Hi,
亂馬客Blog已移到了 「亂馬客 : Re:從零開始的軟體開發生活」
請大家繼續支持 ^_^