delphi监控文件夹
(******************************************
文件和目录监控
当磁盘上有文件或目录操作时,产生事件
使用方法: 开始监控: PathWatch(Self.Handle, 'C:\FtpFolder');
解除监控:PathWatch(-1); 在窗体中加消息监听
private
{ Private declarations }
procedure MsgListern(var Msg:TMessage);message WM_SHNOTIFY; 实现:
procedure TForm1.MsgListern(var Msg:TMessage);
begin
PathWatch(Msg,procedure(a,s1,s2:String) begin
Log('文件事件是:' +a);
Log('文件名称是:' +s1);
Log('另外的参数是:'+s2);
end);
end; ******************************************)
unit PathWatch; interface uses
Winapi.Messages, System.SysUtils, FMX.Types, FMX.Platform.Win, WinAPI.ShlObj,
Winapi.ActiveX, WinApi.Windows, VCL.Dialogs
; const
WM_SHNOTIFY = $401; type
PIDLSTRUCT = ^IDLSTRUCT;
_IDLSTRUCT = record
pidl : PItemIDList;
bWatchSubFolders : Integer;
end;
IDLSTRUCT =_IDLSTRUCT;
type
PSHNOTIFYSTRUCT=^SHNOTIFYSTRUCT;
SHNOTIFYSTRUCT = record
dwItem1 : PItemIDList;
dwItem2 : PItemIDList;
end; Function SHChangeNotifyDeregister(hNotify:integer):integer;stdcall;
external 'Shell32.dll' index 4; Function SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord;lpps:PIDLSTRUCT):integer;stdcall;
external 'Shell32.dll' index 2; function PathWatch(hWND: Integer ; Path:String=''):Boolean; overload;
function PathWatch(hWND: TWindowHandle; Path:String=''):Boolean; overload;
function PathWatch(var Msg: TMessage; callback: TProc<String,String,String>):Boolean; overload; var
g_HSHNotify : Integer;
g_pidlDesktop : PItemIDList;
g_WatchPath : String; implementation function PathWatch(hWND: Integer; Path:String=''):Boolean;
var
ps:PIDLSTRUCT;
begin
result:=False;
Path:=Path.Replace('/','\');
if(hWnd>=0) then begin // 开始监控
g_WatchPath:=Path.ToUpper; if g_HSHNotify = 0 then begin
SHGetSpecialFolderLocation(0, CSIDL_DESKTOP, g_pidlDesktop);
if Boolean(g_pidlDesktop) then begin
getmem(ps,sizeof(IDLSTRUCT));
ps.bWatchSubFolders := 1;
ps.pidl := g_pidlDesktop;
g_HSHNotify := SHChangeNotifyRegister(hWnd, (SHCNF_TYPE Or SHCNF_IDLIST),(SHCNE_ALLEVENTS Or SHCNE_INTERRUPT),WM_SHNOTIFY, 1, ps);
Result := Boolean(g_HSHNotify);
end else CoTaskMemFree(g_pidlDesktop);
end;
end else begin // 解除监控
if boolean(g_HSHNotify) then if Boolean(SHChangeNotifyDeregister(g_HSHNotify)) then begin
g_HSHNotify := 1;
CoTaskMemFree(g_pidlDesktop);
result := True;
end;
end;
end; function PathWatch(hWND: TWindowHandle; Path:String=''):Boolean;
begin
PathWatch(FmxHandleToHWND(hWND),Path); // FireMonkey的窗体不接受处理Windows消息
end; function PathWatch(var Msg: TMessage; callback:TProc<String,String,String>):Boolean;
var
a, s1,s2 : String;
buf : array[0..MAX_PATH] of char;
pidlItem : PSHNOTIFYSTRUCT;
begin
pidlItem :=PSHNOTIFYSTRUCT(Msg.WParam);
SHGetPathFromIDList(pidlItem.dwItem1, buf); s1 := buf;
SHGetPathFromIDList(pidlItem.dwItem2, buf); s2 := buf;
a:='';
case Msg.LParam of
// SHCNE_RENAMEITEM : a := '重命名' ;
SHCNE_CREATE : a := '建立文件' ;
// SHCNE_DELETE : a := '删除文件' ;
// SHCNE_MKDIR : a := '新建目录' ;
// SHCNE_RMDIR : a := '删除目录' ;
// SHCNE_ATTRIBUTES : a := '改变属性' ;
// SHCNE_MEDIAINSERTED : a := '插入介质' ;
// SHCNE_MEDIAREMOVED : a := '移去介质' ;
// SHCNE_DRIVEREMOVED : a := '移去驱动器' ;
// SHCNE_DRIVEADD : a := '添加驱动器' ;
// SHCNE_NETSHARE : a := '改变共享' ;
// SHCNE_UPDATEDIR : a := '更新目录' ;
// SHCNE_UPDATEITEM : a := '更新文件' ;
// SHCNE_SERVERDISCONNECT: a := '断开连接' ;
// SHCNE_UPDATEIMAGE : a := '更新图标' ;
// SHCNE_DRIVEADDGUI : a := '添加驱动器' ;
// SHCNE_RENAMEFOLDER : a := '重命名文件夹' ;
// SHCNE_FREESPACE : a := '磁盘空间改变' ;
// SHCNE_ASSOCCHANGED : a := '改变文件关联' ;
// else a := '其他操作' ; end;
result := True;
if( (a<>'') and (Assigned(callback)) and (s1.ToUpper.StartsWith(g_WatchPath))) and (not s1.Contains('_plate')) then
begin
callback(a,s1,g_WatchPath);
end;
end; end.
调用:
PathWatch(self.Handle, DM.Config.O['Local'].S['PhotoPath']);
窗体中需要消息事件触发:
procedure MsgListern(var Msg: TMessage); message WM_SHNOTIFY; // 触发监听事件
procedure TFormMain.MsgListern(var Msg: TMessage);
begin
PathWatch(Msg, Procedure(act,fn,s2: string) begin
if(act='建立文件') then begin
if SecondsBetween(now(), PrePostTime) >= 5 then //两个时间之间相差的秒数
begin
// 这里处理监控到后 要响应的事情
end;
end;
end);
end;
delphi监控文件夹的更多相关文章
- Python 的 pyinotify 模块 监控文件夹和文件的变动
官方参考: https://github.com/seb-m/pyinotify/wiki/Events-types https://github.com/seb-m/pyinotify/wiki/I ...
- Delphi遍历文件夹及子文件夹(可查找固定格式文件)
Delphi遍历文件夹及子文件夹 {-------------------------------------------------------------------------------过程名 ...
- Storm监控文件夹变化 统计文件单词数量
监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 【.Net 学习系列】-- FileSystemWatcher 监控文件夹新生成文件,并在确认文件没有被其他程序占用后将其移动到指定文件夹
监控文件夹测试程序: using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...
- [转帖]Linux下inotify监控文件夹状态,发生变化后触发rsync同步
Linux下inotify监控文件夹状态,发生变化后触发rsync同步 https://www.cnblogs.com/fjping0606/p/6114123.html 1.安装工具--inotif ...
- 使用FileSystemWatcher监控文件夹及文件
引言 这一周主要精力集中学习一个同事开发的本地文件搜索项目上,其中客户端添加共享文件时主要是使用FileSystemWatcher 监控文件,并在各种事件发生时向服务器发送消息. 解决方法 FileS ...
- Delphi遍历文件夹
/// <remarks> /// 遍历文件夹 (引用SysUtils单元) /// </remarks> procedure TfrmMusicMenu.SearchInDi ...
- DotNetCore跨平台~Quartz热部署的福音~监控文件夹的变化
在DotNetCore出来之后,同时也使用了quartz进行调度中心的设计,将它做到docker里方便部署,在之前的quartz版本里支持配置文件的方式,而现在不支持了,我们应该去想一下,为什么不去支 ...
- Java 学习笔记 观察者模式与适配者模式 监控文件夹
2019.4.12 适配器模式,如果某个类需要实现多个方法,但是开发过程中只需要实现其中的一两个方法,直接使用该类的适配器类,复写需要实现的方法即可 观察者模式:有多个观察者observe观察一个主体 ...
随机推荐
- Restore Nexus 5 to Stock and Flash Factory Images
1.This is the website to download Factory Images for Nexus Devices https://developers.google.com/and ...
- mongodb数据操作(CRUD)
1.数据插入db.集合名.insert() 操作 > use hk switched to db hk > show collections > db.info.insert({&q ...
- 分布式缓存系统 Memcached 哈希表操作
memcached 中有两张hash 表,一个是“主hash 表”(primary_hashtable),另外一个是“原hash 表”(old_hashtable).一般情况下都在主表中接受操作,在插 ...
- Java 数组的三种创建方法,数组拷贝方法
public static void main(String[] args) {//创建数组的第一种方法int[] arr=new int[6];int intValue=arr[5];//Syste ...
- 杂项:UN-APP
ylbtech-杂项:APP 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回顶部 10 ...
- JavaScript之BON
1.windows对象 全局作用域: 2.窗口关系及框架 如果页面包含框架,则每个框架都有自己的window对象,并且保存在iframes集合中,在iframe集合中,可以通过数值索引(从0开始,从左 ...
- hive函数
内置函数 测试各种内置函数的快捷方法: 1.创建一个dual表 create table dual(id string); 2.load一个文件(一行,一个空格)到dual表 3.select sub ...
- Tornado之自定义session
面向对象基础 面向对象中通过索引的方式访问对象,需要内部实现 __getitem__ .__delitem__.__setitem__方法 #!/usr/bin/env python # -*- ...
- sql2012新的系统函数&分析函数
一 .系统函数 1.字符串类函数:不用判断类型和NULL的字符串连接CONCAT函数 SQL Server本来对字符串的连接很简单,直接使用“+”号,但是需要注意两个问题,一是必须类型都是字符串类型, ...
- Spring总结十:事务案例
数据库表Account: 导包: <dependencies> <!--测试--> <dependency> <groupId>junit</gr ...