请问我如何监视整个硬盘文件的变化(不是某个目录, 我要原代码)(200分)

  • 主题发起人 主题发起人 cv_10as
  • 开始时间 开始时间
给你个控件
uses shlobj,Activex;
在uses后面
const
SHCNE_RENAMEITEM=$1;
SHCNE_CREATE=$2;
SHCNE_DELETE=$4;
SHCNE_MKDIR=$8;
SHCNE_RMDIR=$10;
SHCNE_MEDIAINSERTED=$20;
SHCNE_MEDIAREMOVED=$40;
SHCNE_DRIVEREMOVED=$80;
SHCNE_DRIVEADD=$100;
SHCNE_NETSHARE=$400;
SHCNE_ATTRIBUTES=$800;
SHCNE_UPDATEDIR=$1000;
SHCNE_UPDATEITEM=$2000;
SHCNE_SERVERDISCONNECT=$4000;
SHCNE_UPDATEIMAGE=$8000;
SHCNE_DRIVEADDGUI=$10000;
SHCNE_RENAMEFOLDER=$20000;
SHCNE_FREESPACE=$40000;
SHCNE_ASSOCCHANGED=$8000000;
SHCNE_DISKEVENTS=$2381F;
SHCNE_GLOBALEVENTS=$C0581E0;
SHCNE_ALLEVENS=$7FFFFFFF;
SHCNE_INTERRUPT=$80000000;

SHCNF_IDLIST=0;
SHCNF_PATHA=$1;
SHCNF_PRINTERA=$2;
SHCNF_DWORD=$3;
SHCNF_PATHW=$5;
SHCNF_PRINTERW=$6;
SHCNF_TYPE=$FF;

SHCNF_FLUSH=$1000;

SHCNF_FLUSHNOWAIT=$2000;
SHCNF_PATH=SHCNF_PATHW;
SHCNF_PRINTER=SHCNF_PRINTERW;

WM_SHNOTIFY=$401;
NOERROR=0;

private
procedure WMShellReg(var Message:TMessage);message WM_SHNOTIFY;
在var之前

type PSHNOTIFYSTRUCT=^SHNOTIFYSTRUCT;
SHNOTIFYSTRUCT=record
dwItem1:PItemIDList;
dwItem2:PItemIDList;
end;

Type PSHFileInfoByte=^SHFileInfoByte;
_SHFileInfoByte=record
hIcon:Integer;
iIcon:Integer;
dwAttributes:Integer;
szDisplayName:array[0..259]of char;
szTypeName:array[0..79]of char;
end;
SHFileInfoByte=_SHFileInfoByte;

Type PIDLSTRUCT=^IDLSTRUCT;
_IDLSTRUCT=record
pidl:PItemIDList;
bWatchSubFolders:Integer;
end;

IDLSTRUCT= _IDLSTRUCT;

function SHNotify_Register(hWnd:Integer):Boolean;
function SHNotify_UnRegister:Boolean;

function SHEventName(strPath1,strPath2:string;lParam:Integer):string;

function SHChangeNotifyDeregister(hNotity: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 SHGetFileInfoPidl(pidl:PItemIDList;
dwFileAttributes:Integer;
psfid:PSHFILEINFOBYTE;
cbFileInfo:Integer;
uFlags:Integer):Integer;stdcall;
external 'Shell32.dll' name 'SHGetFileInfoA';

在var 之后
m_hSHNotify:Integer;
m_pidlDesktop:PItemIDList;

在implementation之后添加函数
function SHEventName(strPath1,strPath2:string;lParam:Integer):string;
var
sEvent:String;
begin
case lParam of
SHCNE_RENAMEITEM:sEvent:='重命名文件'+strPath1+'为'+strPath2;
SHCNE_CREATE:sEvent:='建立文件 文件名'+strPath1;
SHCNE_DELETE:sEvent:='删除文件 文件名'+strPath1;
SHCNE_MKDIR:sEvent:='新建目录 目录名'+strPath1;
SHCNE_RMDIR:sEvent:='删除目录 目录名'+strPath1;
SHCNE_MEDIAINSERTED:sEvent:=strPath1+'插入可移动盘';
SHCNE_MEDIAREMOVED: sEvent:=strPath1+'移去盘'+strPath1+' '+strpath2;

SHCNE_DRIVEREMOVED:sEvent:='移去驱动器'+strPath1;
SHCNE_DRIVEADD:sEvent:='添加驱动器'+strPath1;
SHCNE_NETSHARE:sEvent:='改变目录'+strpath1+'的共享属性';
SHCNE_ATTRIBUTES:sEvent:='改变文件目录属性文件名'+strpath1;
SHCNE_UPDATEDIR:sEvent:='更新目录'+strPath1;
SHCNE_UPDATEITEM:sEvent:='更新文件文件名'+strPath1;
SHCNE_SERVERDISCONNECT:sEvent:='断开于服务器的连接'+strPath1+' '+strPath2;
SHCNE_UPDATEiMAGE:sEvent:='SHCNE_UPDATEiMAGE';
SHCNE_DRIVEADDGUI:sEvent:='SHCNE_DRIVEADDGUI';
SHCNE_RENAMEFOLDER:sEvent:='重命名文件夹'+strPath1+' 为'+strPath2;
SHCNE_FREESPACE:sEvent:='磁盘空间大小改变';
SHCNE_ASSOCCHANGED:sEvent:='改变文件关连';
else
sEvent:='未知操作'+IntToStr(lParam);
end;

Result:=sEvent+DateTimeToStr(Now);
end;


function SHNotify_Register(hWnd:Integer):Boolean;
var
ps:PIDLSTRUCT;
begin
{$R-}
Result:=false;
if m_hSHNotify=0 then
begin
if SHGetSpecialFolderLocation(0,CSIDL_DESKTOP,m_pidlDesktop) <> NOERROR then
Form1.Close;
if Boolean(m_pidlDesktop) then
begin
ps.bWatchSubFolders:=1;
ps.pidl:=m_pidlDesktop;

m_hSHNotify:=SHChangeNotifyRegister(hWnd,(SHCNF_TYPE OR SHCNF_IDLIST),
(SHCNE_ALLEVENTS OR SHCNE_INTERRUPT),
WM_SHNOTIFY,1,ps);
Result:=Boolean(m_hSHNotify);
end
else
CoTaskMemFree(m_pidlDesktop);
end;
{R+}
end;

function SHNotify_UnRegister:Boolean;
begin
Result:=false;
if Boolean(m_hSHNotify)then
if Boolean(SHChangeNotifyDeregister(m_hSHNotify) ) then
begin
m_hSHNotify:=0;
CoTaskMemFree(m_pidlDesktop);
Result:=true;
end;
end;

procedure TForm1.WMShellReg(var Message:TMessage);
var
strPath1,strPath2:String;
charPath:array[0..259]of char;
pidlItem:PSHNOTIFYSTRUCT;
begin
pidlItem:=PSHNOTIFYSTRUCT(Message.WParam);
SHGetPathFromIDList(pidlItem.dwItem1,charPath);
strPath1:=charPath;
SHGetPathFromIDList(pidlItem.dwItem2,charPath);
strPath2:=charPath;
Memo1.Lines.Add( SHEventName(strPath1,strPath2,Message.lParam)+chr(13)+chr(10));
end;


当退出时候
if Boolean(m_pidlDesktop)then
SHNotify_Unregister;

注册
m_hSHNotify:=0;
if SHNotify_Register(Form1.Handle) then
begin

ShowMessage('注册成功');
Button1.Enabled:=false;
end
else
ShowMessage('注册失败');
 
谢谢可以使用的
 
后退
顶部