ReadDirectoryChangesw使用问题
现在问题是
重命名 无法获取新名字
修改文件 无法判断是修改数据 还是修改属性
还会多余很多东西 重复显示 代码如下 谢谢各位大神帮帮忙
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls,FileCtrl,StrUtils;
type
PFileNotifyInformation = ^FILE_NOTIFY_INFORMATION;
FILE_NOTIFY_INFORMATION = Record
NextEntryOffset: DWORD;
Action: DWORD;
FileNameLength: DWORD;
FileName: Array[0..1024] Of WideChar;
end;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
ListView1: TListView;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
DirectoryHadle:Integer;
Pinfo:FILE_NOTIFY_INFORMATION;
IO:OVERLAPPED;
Kz:Boolean;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure Directory;stdcall;
var
jieguo:Boolean;
str1,str2,str3:string;
Additem:TListItem;
begin
while Form1.Kz=True do begin
jieguo:=ReadDirectoryChangesw(form1.DirectoryHadle,@Form1.Pinfo,1024,True
,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,nil,@form1.IO,nil);
if jieguo=True then begin
str1:='';
str2:='';
if form1.Pinfo.NextEntryOffset<>0 then begin
str3:=WideCharToString(Form1.Pinfo.FileName);
str2:=LeftStr(str3,Form1.Pinfo.FileNameLength);
str1:=Copy(str3,Form1.Pinfo.NextEntryOffset+1,length(str3)-form1.Pinfo.NextEntryOffset);
end
else
begin
str3:=WideCharToString(Form1.Pinfo.FileName);
str1:=LeftStr(str3,Form1.Pinfo.FileNameLength);
end;
fillchar(form1.Pinfo.FileName, sizeof(form1.Pinfo.FileName), 0);
if Form1.Pinfo.Action=2 then begin
Additem:=form1.ListView1.Items.Add;
Additem.Caption:=IntToStr(Additem.Index+1);
Additem.SubItems.Add(DateTimeToStr(now));
Additem.SubItems.Add('文件已删除');
Additem.SubItems.Add(form1.Edit1.Text+str1);
end
else
begin
if Form1.Pinfo.Action=1 then begin
Additem:=form1.ListView1.Items.Add;
Additem.Caption:=IntToStr(Additem.Index+1);
Additem.SubItems.Add(DateTimeToStr(now));
Additem.SubItems.Add('文件已创建');
Additem.SubItems.Add(form1.Edit1.Text+str1);
end
else
begin
if (Form1.Pinfo.Action=4) and (str2<>'') then begin
Additem:=form1.ListView1.Items.Add;
Additem.Caption:=IntToStr(Additem.Index+1);
Additem.SubItems.Add(DateTimeToStr(now));
Additem.SubItems.Add('文件已重命名');
Additem.SubItems.Add(form1.Edit1.Text+str2+'->'+form1.Edit1.Text+str1);
str1:='';
str2:='';
end
else
begin
if (Form1.Pinfo.Action=3) and (str1<>'') and (str2<>'') then begin
Additem:=form1.ListView1.Items.Add;
Additem.Caption:=IntToStr(Additem.Index+1);
Additem.SubItems.Add(DateTimeToStr(now));
Additem.SubItems.Add('文件已修改');
Additem.SubItems.Add(form1.Edit1.Text+str1);
end
else
begin
Additem:=form1.ListView1.Items.Add;
Additem.Caption:=IntToStr(Additem.Index+1);
Additem.SubItems.Add(DateTimeToStr(now));
Additem.SubItems.Add('文件已改变');
Additem.SubItems.Add(form1.Edit1.Text+str1);
end;
end;
end;
end;
end
else
begin
ShowMessage('出现致命错误,请重新运行!');
Halt(0);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
strCaption,strDirectory:String;
begin
begin
strCaption:='请选择要监视的目录';
SelectDirectory(strCaption,'',strDirectory);
if strDirectory='' then Exit;
if RightStr(strDirectory,1) <> '\' then
begin
Edit1.Text:=strDirectory+'\';
end
else
begin
Edit1.Text:=strDirectory;
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
ThreadId:Cardinal;
begin
if Button2.Caption='开始监视' then begin
DirectoryHadle:= CreateFile( PWideChar(Edit1.Text), GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE,
nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
if DirectoryHadle=-1 then begin
ShowMessage('请选择正确目录');
exit;
end;
KZ:=True;
CreateThread(nil,0,@Directory,nil,0,ThreadId);
Button2.Caption:='停止监视';
Edit1.Enabled:=False;
end
else
begin
Button2.Caption:='开始监视' ;
Form1.Edit1.Enabled:=true;
kz:=False;
end;
end;
end.