不知道哪里有问题啊 高手们帮忙看下unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FindFile(APath: String;FList: TStrings);
function GetDirectoryName(Dir: String): String;
function IsDirNotation(ADirName: String): Boolean;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
s:Tstrings;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
findfile('d:\haha\',s);
end;procedure tform1.FindFile(APath: String;FList: TStrings);
var
FSearchRec,DSearchRec: TSearchRec;
FindResult: integer;
begin
APath:=getdirectoryname(APath);
FindResult := FindFirst(APath + '*.*',Faanyfile-faDirectory,FSearchRec);
try
while FindResult = 0 do
begin
FList.Add(APath+FSearchRec.name);
FindResult := FindNext(FSearchRec);
end; FindResult := FindFirst(APath + '*.*',faDirectory,DSearchRec); while FindResult = 0 do
begin
if ((DSearchRec.Attr and faDirectory) = faDirectory) and (not isDirNotation(DSearchRec.Name)) then
FindFile(APath + DSearchRec.Name, FList);
FindResult := FindNext(DSearchRec);
end;
finally
FindClose(FSearchRec);
memo1.lines:=FList;
end;
end;function Tform1.GetDirectoryName(Dir: String): String;
begin
if Dir[Length(Dir)] <> '\' then
Result := Dir + '\'
else
Result := Dir;
end;function tform1.IsDirNotation(ADirName: String): Boolean;
begin
Result := (ADirName = '.') or (ADirName = '..');
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FindFile(APath: String;FList: TStrings);
function GetDirectoryName(Dir: String): String;
function IsDirNotation(ADirName: String): Boolean;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
s:Tstrings;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
findfile('d:\haha\',s);
end;procedure tform1.FindFile(APath: String;FList: TStrings);
var
FSearchRec,DSearchRec: TSearchRec;
FindResult: integer;
begin
APath:=getdirectoryname(APath);
FindResult := FindFirst(APath + '*.*',Faanyfile-faDirectory,FSearchRec);
try
while FindResult = 0 do
begin
FList.Add(APath+FSearchRec.name);
FindResult := FindNext(FSearchRec);
end; FindResult := FindFirst(APath + '*.*',faDirectory,DSearchRec); while FindResult = 0 do
begin
if ((DSearchRec.Attr and faDirectory) = faDirectory) and (not isDirNotation(DSearchRec.Name)) then
FindFile(APath + DSearchRec.Name, FList);
FindResult := FindNext(DSearchRec);
end;
finally
FindClose(FSearchRec);
memo1.lines:=FList;
end;
end;function Tform1.GetDirectoryName(Dir: String): String;
begin
if Dir[Length(Dir)] <> '\' then
Result := Dir + '\'
else
Result := Dir;
end;function tform1.IsDirNotation(ADirName: String): Boolean;
begin
Result := (ADirName = '.') or (ADirName = '..');
end;end.
FSearchRec对象是不是不存在啊?
s没有创建,访问时为空
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FindFile(APath: String;FList: TStrings);
function GetDirectoryName(Dir: String): String;
function IsDirNotation(ADirName: String): Boolean;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
s:Tstrings;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
S := TStringlist.create;
findfile('d:\haha\',s);
end;procedure tform1.FindFile(APath: String;FList: TStrings);
var
FSearchRec,DSearchRec: TSearchRec;
FindResult: integer;
begin
APath:=getdirectoryname(APath);
FindResult := FindFirst(APath + '*.*',Faanyfile-faDirectory,FSearchRec);
try
while FindResult = 0 do
begin
FList.Add(APath+FSearchRec.name);
FindResult := FindNext(FSearchRec);
end; FindResult := FindFirst(APath + '*.*',faDirectory,DSearchRec); while FindResult = 0 do
begin
if ((DSearchRec.Attr and faDirectory) = faDirectory) and (not isDirNotation(DSearchRec.Name)) then
FindFile(APath + DSearchRec.Name, FList);
FindResult := FindNext(DSearchRec);
end;
finally
FindClose(FSearchRec);
memo1.lines:=FList;
end;
end;function Tform1.GetDirectoryName(Dir: String): String;
begin
if Dir[Length(Dir)] <> '\' then
Result := Dir + '\'
else
Result := Dir;
end;function tform1.IsDirNotation(ADirName: String): Boolean;
begin
Result := (ADirName = '.') or (ADirName = '..');
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FindFile(APath: String);
function GetDirectoryName(Dir: String): String;
function IsDirNotation(ADirName: String): Boolean;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.clear;
findfile('d:\haha\');
end;procedure tform1.FindFile(APath: String);
var
FSearchRec,DSearchRec: TSearchRec;
FindResult: integer;
begin
APath:=getdirectoryname(APath);
FindResult := FindFirst(APath + '*.*',Faanyfile-faDirectory,FSearchRec);
try
while FindResult = 0 do
begin
memo1.lines.Add(APath+FSearchRec.name);
FindResult := FindNext(FSearchRec);
end; FindResult := FindFirst(APath + '*.*',faDirectory,DSearchRec); while FindResult = 0 do
begin
if ((DSearchRec.Attr and faDirectory) = faDirectory) and (not isDirNotation(DSearchRec.Name)) then
FindFile(APath + DSearchRec.Name);
FindResult := FindNext(DSearchRec);
end;
finally
FindClose(FSearchRec);
end;
end;function Tform1.GetDirectoryName(Dir: String): String;
begin
if Dir[Length(Dir)] <> '\' then
Result := Dir + '\'
else
Result := Dir;
end;function tform1.IsDirNotation(ADirName: String): Boolean;
begin
Result := (ADirName = '.') or (ADirName = '..');
end;end.自己测试
FormCreate事件里面写:
s := TStringList.Create;FormDestroy事件里写:
s.Free;