2010-12-22 85 views
1

如何檢查文件是否專門鎖定在AutoIt中?我不是在談論讀/寫訪問。另外,我做了一些研究,如果文件被鎖定,它不會顯示在任務管理器進程列表中。獨家鎖定文件檢測AutoIt

Perl的一個例子叫做flock: 您檢查文件是否通過$ theRC = flock($ HANDLE,LOCK_EX | LOCK_NB)鎖定;

我想在AutoIt中複製這個。

我已經找到一個可行的解決方案:

Local $f = "C:/log.txt" 

MsgBox(0, _FileInUse($f), @error) 

;=============================================================================== 
; 
; Function Name: _FileInUse() 
; Description:  Checks if file is in use 
; Parameter(s):  $sFilename = File name 
; Return Value(s): 1 - file in use (@error contains system error code) 
;     0 - file not in use 
; 
;=============================================================================== 
Func _FileInUse($sFilename) 
    Local $aRet, $hFile 
    $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _ 
            "str", $sFilename, _ ;lpFileName 
            "dword", 0x80000000, _ ;dwDesiredAccess = GENERIC_READ 
            "dword", 0, _ ;dwShareMode = DO NOT SHARE 
            "dword", 0, _ ;lpSecurityAttributes = NULL 
            "dword", 3, _ ;dwCreationDisposition = OPEN_EXISTING 
            "dword", 128, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL 
            "hwnd", 0) ;hTemplateFile = NULL 
    $hFile = $aRet[0] 
    If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1 
     $aRet = DllCall("Kernel32.dll", "int", "GetLastError") 
     SetError($aRet[0]) 
     Return 1 
    Else 
     ;close file handle 
     DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile) 
     Return 0 
    EndIf 
EndFunc 
+2

呸沒有語法機器人爲AutoIt的? – 2010-12-22 20:54:19

+0

突出顯示您的代碼並單擊編輯器工具欄中的「{}」圖標。 – aphoria 2010-12-23 13:44:43

回答

1

這應該做的伎倆:

Func FileInUse($filename) 
    $handle = FileOpen($filename, 1) 

    $result = False 
    if $handle = -1 then $result = True 

    FileClose($handle) 

    return $result 
EndFunc 

;~ usage 
$filename = "C:\Windu15f.exe" 
if FileInUse($filename) Then 
    MsgBox(0, "", "File is in use") 
Else 
    MsgBox(0, "", "Not in use - go nuts") 
EndIf