函数参考


_WinAPI_FindFirstChangeNotification

Creates a change notification handle and sets up initial change notification filter conditions.

#Include <WinAPIEx.au3>
_WinAPI_FindFirstChangeNotification ( $sDirectory, $iFlags [, $fSubtree] )

参数

$sDirectory The full path of the directory to be watched.
$iFilter The filter conditions that satisfy a change notification wait. This parameter can be one or more of the
following values.

$FILE_NOTIFY_CHANGE_FILE_NAME
$FILE_NOTIFY_CHANGE_DIR_NAME
$FILE_NOTIFY_CHANGE_ATTRIBUTES
$FILE_NOTIFY_CHANGE_SIZE
$FILE_NOTIFY_CHANGE_LAST_WRITE
$FILE_NOTIFY_CHANGE_SECURITY
$fSubtree [可选参数] Specifies whether to monitor the subdirectories of the specified directory, valid values:
TRUE - Monitor the directory tree rooted at the specified directory.
FALSE - Monitor only the specified directory. (Default)

返回值

Success A handle to a find change notification object.
失败: 返回 0 并设置 @error 标志为非 0 值.

注意/说明

The _WinAPI_Wait... functions can monitor the specified directory or subtree by using the handle returned by this
function. A wait is satisfied when one of the filter conditions occurs in the monitored directory or subtree.

After the wait has been satisfied, the application can respond to this condition and continue monitoring the directory
by calling the _WinAPI_FindNextChangeNotification() function and the appropriate wait function. When the handle is
no longer needed, it can be closed by using the _WinAPI_FindCloseChangeNotification() function.

Notifications may not be returned when calling _WinAPI_FindFirstChangeNotification() for a remote file system.

相关

详情参考

在MSDN中搜索


示例/演示


#Include <APIConstants.au3>
#Include <WinAPIEx.au3>

Opt('MustDeclareVars', 1)
Opt('TrayAutoPause', 0)

Global Const $sPath = @ScriptDir & '\~TEST~'

Global $hObj[2], $tObj, $pObj, $ID

DirCreate($sPath)
If Not FileExists($sPath) Then
    MsgBox(16, 'Error', 'Unable to create folder.')
    Exit
EndIf

OnAutoItExitRegister('OnAutoItExit')

$hObj[0] = _WinAPI_FindFirstChangeNotification($sPath, $FILE_NOTIFY_CHANGE_FILE_NAME)
$hObj[1] = _WinAPI_FindFirstChangeNotification($sPath, $FILE_NOTIFY_CHANGE_DIR_NAME)

If (Not $hObj[0]) Or (Not $hObj[1]) Then
    MsgBox(16, 'Error', 'Unable to create change notification.')
    Exit
EndIf

$tObj = DllStructCreate('ptr;ptr')
$pObj = DllStructGetPtr($tObj)
For $i = 0 To 1
    DllStructSetData($tObj, $i + 1, $hObj[$i])
Next

While 1
    Sleep(100)
    $ID = _WinAPI_WaitForMultipleObjects(2, $pObj, 0, 0)
    Switch $ID
        Case 0 ; WAIT_OBJECT_0
            ConsoleWrite('A file was created, renamed, or deleted in the directory.' & @CR)
        Case 1 ; WAIT_OBJECT_0 + 1
            ConsoleWrite('A directory was created, renamed, or deleted.' & @CR)
        Case Else
            ContinueLoop
    EndSwitch
    If Not _WinAPI_FindNextChangeNotification($hObj[$ID]) Then
        MsgBox(16, 'Error', 'Unexpected error.')
        Exit
    EndIf
WEnd

Func OnAutoItExit()
    For $i = 0 To 1
        If $hObj[$i] Then
            _WinAPI_FindCloseChangeNotification($hObj[$i])
        EndIf
    Next
;   DirRemove($sPath)
EndFunc   ;==>OnAutoItExit