//你可以自己试试知道错了,为什么?unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, AppEvnts;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
//删除目录 isError为出错时是否提示出错信息,为真时提示,否则退出过程不提示 create by xsw
procedure deletedir(Mpath:string; IsError:boolean);
var
vSearchRec: TSearchRec;
K: Integer;
begin
try
try
K := FindFirst(Mpath+'\*.*', faAnyFile, vSearchRec);
while K = 0 do
begin
if (vSearchRec.Attr and faDirectory > 0) and (Pos(vSearchRec.Name, '..') = 0) then
begin
deletedir(mPath + '\' + vSearchRec.Name, IsError);
rmdir(pchar(mPath + '\' + vSearchRec.Name));//删除目录
end else if Pos(vSearchRec.Name, '..') = 0 then
begin
//FileSetAttr(pchar(vSearchRec.Name),0); //取消文件为普通属性
DeleteFile(Mpath + '\'+vSearchRec.Name); //删除文件
end; K := FindNext(vSearchRec);
end; finally
FindClose(vSearchRec);
end;
Rmdir(Mpath); //删除目录 (加入这名后总会引起reset(F:fileName)函数出现错误)
except
on E:exception do
if IsError then
messagebox(application.handle,pchar('不好意思了,出错了!'+#13#10
+'出错原因:'+E.Message+#13#10#10
+'如有必要,请联络程序供应商!'),'温馨提示', mb_iconError);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
s:Textfile;
str:string;
begin
str:='H:\hyde\a.txt';
chdir(pchar('H:\hyde\'));
AssignFIle(s,str);
try
reset(s);
showmessage('ok');
finally
closefile(S);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
deletedir('H:\b',false);
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, AppEvnts;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
//删除目录 isError为出错时是否提示出错信息,为真时提示,否则退出过程不提示 create by xsw
procedure deletedir(Mpath:string; IsError:boolean);
var
vSearchRec: TSearchRec;
K: Integer;
begin
try
try
K := FindFirst(Mpath+'\*.*', faAnyFile, vSearchRec);
while K = 0 do
begin
if (vSearchRec.Attr and faDirectory > 0) and (Pos(vSearchRec.Name, '..') = 0) then
begin
deletedir(mPath + '\' + vSearchRec.Name, IsError);
rmdir(pchar(mPath + '\' + vSearchRec.Name));//删除目录
end else if Pos(vSearchRec.Name, '..') = 0 then
begin
//FileSetAttr(pchar(vSearchRec.Name),0); //取消文件为普通属性
DeleteFile(Mpath + '\'+vSearchRec.Name); //删除文件
end; K := FindNext(vSearchRec);
end; finally
FindClose(vSearchRec);
end;
Rmdir(Mpath); //删除目录 (加入这名后总会引起reset(F:fileName)函数出现错误)
except
on E:exception do
if IsError then
messagebox(application.handle,pchar('不好意思了,出错了!'+#13#10
+'出错原因:'+E.Message+#13#10#10
+'如有必要,请联络程序供应商!'),'温馨提示', mb_iconError);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
s:Textfile;
str:string;
begin
str:='H:\hyde\a.txt';
chdir(pchar('H:\hyde\'));
AssignFIle(s,str);
try
reset(s);
showmessage('ok');
finally
closefile(S);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
deletedir('H:\b',false);
end;end.
检查这个声明
var
s:Textfile;
str:string;
begin
str:='c:\naner\a.txt';
chdir(pchar('c:\naner\'));
if fileexists(str) then //判断文件是否存在
begin
try
AssignFIle(s,str); //这一步是将文件名赋给s
reset(s);
showmessage('ok');
finally
closefile(S);
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
s:Textfile;
str:string;
begin
str:='c:\naner\a.txt';
Chdir(pchar('c:\naner\'));
AssignFIle(s,str); //将文件名与文件关联
try
if FileExists(str) then
begin
FileMode := 0;
Reset(s);
CloseFile(S);
end;
showmessage('ok');
except
end;
end;