Y
ynduanlian
Unregistered / Unconfirmed
GUEST, unregistred user!
在网上Google,结果得到了这样的解答: 当一个固定文件夹下多了一个文件,怎样让程序立即知道,并作出响应! 主要解答者: zswangII 提交人: hthunter 感谢: sailer_shi、xinshiji、cqbonny、dyzg 审核者: aiirii 社区对应贴子: 查看 A : 当某个固定目录下,多了一个文件,怎样让程序立即知道,并作出响应!最好不要用Ttimer! --------------------------------------------------------------- 回复人: zswangII(伴水清清)(职业清洁工) ( ) 信誉:110 2003-12-29 16:52:28 得分:150 //通过ShellAPI函数来实现比较科学~~ //注册一个事件响应~~ //代码如下~~ Delphi(Pascal) codeunit TestNotify; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ShlObj, ShellAPI; const WM_SHNOTIFY = WM_USER + 10; type TFormTestNotify = class(TForm) MemoNotifyLog: TMemo; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } FNotifyHandle: THandle; procedure WMSHNOTIFY(var Msg: TMessage); message WM_SHNOTIFY; public { Public declarations } end; var FormTestNotify: TFormTestNotify; implementation {$R *.dfm} type NOTIFYREGISTER = packed record pidlPath: PItemIDList; bWatchSubtree: BOOL; end; PNotifyRegister = ^NOTIFYREGISTER; {$WARNINGS OFF} function SHChangeNotifyRegister(hWnd: HWND; dwFlags: Integer; wEventMask: Cardinal; uMsg: UINT; cItems: Integer; lpItems: PNotifyRegister): HWND; stdcall; external Shell32 index 2; function SHChangeNotifyDeregister(hWnd: HWND): Boolean; stdcall; external Shell32 index 4; function SHILCreateFromPath(pszPath: PChar; ppidl: PItemIDList; rgflnOut: PDWORD): HResult; stdcall; external Shell32 index 28; {$WARNINGS ON} procedure TFormTestNotify.FormCreate(Sender: TObject); var vNotifyRegister: NOTIFYREGISTER; vAttributes: WORD; vItemIDList: PItemIDList; begin SHILCreatefromPath('c:/temp', @vItemIDList, @vAttributes); vNotifyRegister.pidlPath := vItemIDList; vNotifyRegister.bWatchSubtree := True; FNotifyHandle := SHChangeNotifyRegister(Handle, SHCNF_TYPE or SHCNF_IDLIST, SHCNE_ALLEVENTS or SHCNE_INTERRUPT, WM_SHNOTIFY, 1, @vNotifyRegister); MemoNotifyLog.Clear; end; procedure TFormTestNotify.FormDestroy(Sender: TObject); begin SHChangeNotifyDeregister(FNotifyHandle); end; procedure TFormTestNotify.WMSHNOTIFY(var Msg: TMessage); type PSHNOTIFYSTRUCT = ^SHNOTIFYSTRUCT; SHNOTIFYSTRUCT = packed record dwItem1: PItemIDList; dwItem2: PItemIDList; end; var vBuffer: array[0..MAX_PATH] of Char; pidlItem: PSHNOTIFYSTRUCT; S: string; begin pidlItem := PSHNOTIFYSTRUCT(Msg.wParam); SHGetPathFromIDList(pidlItem.dwItem1, vBuffer); S := vBuffer; SHGetPathFromIDList(pidlItem.dwItem2, vBuffer); case Msg.lParam of //根据参数设置提示消息 SHCNE_RENAMEITEM: S := '重命名文件' + S + '为' + vBuffer; SHCNE_CREATE: S := '建立文件 文件名:' + S; SHCNE_DELETE: S := '删除文件 文件名:' + S; SHCNE_MKDIR: S := '新建目录 目录名:' + S; SHCNE_RMDIR: S := '删除目录 目录名:' + S; SHCNE_MEDIAINSERTED: S := S + '中插入可移动存储介质'; SHCNE_MEDIAREMOVED: S := S + '中移去可移动存储介质' + S + ' ' + vBuffer; SHCNE_DRIVEREMOVED: S := '移去驱动器' + S; SHCNE_DRIVEADD: S := '添加驱动器' + S; SHCNE_NETSHARE: S := '改变目录' + S + '的共享属性'; SHCNE_ATTRIBUTES: S := '改变文件目录属性 文件名' + S; SHCNE_UPDATEDIR: S := '更新目录' + S; SHCNE_UPDATEITEM: S := '更新文件 文件名:' + S; SHCNE_SERVERDISCONNECT: S := '断开与服务器的连接' + S + ' ' + vBuffer; SHCNE_UPDATEIMAGE: S := 'SHCNE_UPDATEIMAGE'; SHCNE_DRIVEADDGUI: S := 'SHCNE_DRIVEADDGUI'; SHCNE_RENAMEFOLDER: S := '重命名文件夹' + S + '为' + vBuffer; SHCNE_FREESPACE: S := '磁盘空间大小改变'; SHCNE_ASSOCCHANGED: S := '改变文件关联'; else S := '未知操作' + IntToStr(Msg.lParam); end; MemoNotifyLog.Lines.Add(S); end; end. 这个代码的确可以监视磁盘变化,但是存在的问题是:我希望监视的目录是'c:/temp',但是代码实际执行的效果是别的目录、甚至别的磁盘驱动器发生了事件也会被监视,这是怎么回事?哪的问题?