实现对已经存在的盘监视已经实现,但问题是a盘没法监视,除非a盘里面有盘,不然无法得到正确的句柄,谁知道如何或者a盘中有盘的信息?如果做定时器去检测可能太耗资源了,因为a盘会一直响应,下面是一部分实现的代码<br>//监控目录<br>procedure tform1.jkmu(path:string);<br>begin<br> FNotifyFilter := 0;<br> FNotifyFilter:=<br> FILE_NOTIFY_CHANGE_FILE_NAME or<br> FILE_NOTIFY_CHANGE_DIR_NAME or<br> FILE_NOTIFY_CHANGE_ATTRIBUTES or<br> FILE_NOTIFY_CHANGE_SIZE or<br> FILE_NOTIFY_CHANGE_LAST_WRITE or<br> FILE_NOTIFY_CHANGE_LAST_ACCESS or<br> FILE_NOTIFY_CHANGE_CREATION or<br> FILE_NOTIFY_CHANGE_SECURITY;<br> if FNotifyFilter = 0 then<br> begin<br> exit;<br> end;<br><br> FDirectoryHandle := CreateFile(PChar(path),<br> FILE_LIST_DIRECTORY,<br> FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE,<br> nil,<br> OPEN_EXISTING,<br> FILE_FLAG_BACKUP_SEMANTICS or FILE_FLAG_OVERLAPPED,<br> 0);<br> if FDirectoryHandle = INVALID_HANDLE_VALUE then<br> begin<br> beep;<br> FDirectoryHandle := 0;<br> ShowMessage(SysErrorMessage(GetLastError));<br> exit;<br> end;<br> FCompletionPort := CreateIoCompletionPort(FDirectoryHandle, 0, Longint(pointer(self)), 0);<br> ZeroMemory(@FNotificationBuffer, SizeOf(FNotificationBuffer));<br> FBytesWritten := 0;<br> if not ReadDirectoryChanges(FDirectoryHandle, @FNotificationBuffer,<br> SizeOf(FNotificationBuffer), true,<br> FNotifyFilter, @FBytesWritten, @FOverlapped, nil) then<br> begin<br> CloseHandle(FDirectoryHandle);<br> FDirectoryHandle := 0;<br> CloseHandle(FCompletionPort);<br> FCompletionPort := 0;<br> ShowMessage(SysErrorMessage(GetLastError));<br> exit;<br> end;<br><br> FWatchThread := TWaitThread.Create(self);<br> TWaitThread(FWatchThread).Resume;<br>end;<br>//停止监控目录<br>procedure tform1.stopjkmu();<br>begin<br>if FCompletionPort = 0 then<br> exit;<br> PostQueuedCompletionStatus(FCompletionPort, 0, 0, nil);<br> FWatchThread.WaitFor;<br> FWatchThread.Free;<br> CloseHandle(FDirectoryHandle);<br> FDirectoryHandle := 0;<br> CloseHandle(FCompletionPort);<br> FCompletionPort := 0;<br>end;<br><br>constructor TWaitThread.Create(Form: TForm1);<br>begin<br> inherited Create(True);<br> FForm := Form;<br> FreeOnTerminate := False;<br>end;<br><br>procedure TWaitThread.HandleEvent;<br>var<br> FileOpNotification: PFileNotifyInformation;<br> Offset: Longint;<br>begin<br> with FForm do<br> begin<br> Pointer(FileOpNotification) := @FNotificationBuffer[0];<br> repeat<br> Offset := FileOpNotification^.NextEntryOffset;<br> writelog(<br> Format(SAction[FileOpNotification^.Action],<br> [WideCharToString(@(FileOpNotification^.FileName))])<br> ,'wjjk');<br> PChar(FileOpNotification) := PChar(FileOpNotification)+Offset;<br> until Offset=0;<br> end;<br>end;<br><br>procedure TWaitThread.Execute;<br>var<br> numBytes: DWORD;<br> cbOffset: DWORD;<br> CompletionKey: DWORD;<br>begin<br> while not Terminated do<br> begin<br> GetQueuedCompletionStatus( FForm.FCompletionPort, numBytes, CompletionKey, FForm.FPOverlapped, INFINITE);<br> if CompletionKey &lt;&gt; 0 then<br> begin<br> Synchronize(HandleEvent);<br> with FForm do<br> begin<br> FBytesWritten := 0;<br> ZeroMemory(@FNotificationBuffer, SizeOf(FNotificationBuffer));<br> ReadDirectoryChanges(FDirectoryHandle, @FNotificationBuffer, SizeOf(FNotificationBuffer), true, FNotifyFilter, @FBytesWritten, @FOverlapped, nil);<br> end;<br> end<br> else<br> Terminate;<br> end;<br>end;