5,713
社区成员
代码如下:
procedure TForm1.ToolButton4Click(Sender: TObject);
var
FD:Thandle;
BytesToWrite: DWORD;
Buffer:array [0..1023] of byte;
overlapped:TOverlapped;
begin
FillChar(Buffer,sizeof(Buffer),0);
FD := CreateFile(PChar('\\?\D:\PC游戏\Diablo\Save\'), GENERIC_READ or GENERIC_WRITE or FILE_LIST_DIRECTORY , FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE, nil,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS or FILE_FLAG_OVERLAPPED, 0);
FillChar(overlapped,sizeof(TOverlapped),0);
Overlapped.hEvent := CreateEvent(Nil, True, True, Nil);
BytesToWrite := SizeOf(TFileChangeInfo);
ReSetEvent(Overlapped.hEvent);
if ReadDirectoryChangesW(FD, @Buffer, SizeOf(Buffer), false,
FILE_NOTIFY_CHANGE_FILE_NAME or FILE_NOTIFY_CHANGE_DIR_NAME or
FILE_NOTIFY_CHANGE_ATTRIBUTES or FILE_NOTIFY_CHANGE_SIZE or
FILE_NOTIFY_CHANGE_LAST_WRITE or FILE_NOTIFY_CHANGE_LAST_ACCESS or
FILE_NOTIFY_CHANGE_CREATION or FILE_NOTIFY_CHANGE_SECURITY,
@BytesToWrite, @overlapped, nil) then
begin
Caption :=Inttostr(GetLastError);
WaitForSingleObject(Overlapped.hEvent, INFINITE); // 等待事件发生
end
else Caption :=Inttostr(GetLastError);
CloseHandle(FD);
CloseHandle(Overlapped.hEvent);
end;
当监视目录下某个文件内容变化时,WaitForSingleObject(Overlapped.hEvent, INFINITE);无法等待到hEvent事件,也就是hEvent一直阻塞,主程序一直阻塞,请问ReadDirectoryChangesW如何监控文件变化?
最后一个参数是回调函数~~~