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
呸没有语法机器人为AutoIt的? – 2010-12-22 20:54:19
突出显示您的代码并单击编辑器工具栏中的“{}”图标。 – aphoria 2010-12-23 13:44:43