unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormShow(Sender: TObject);
var
SearchRec: TSearchRec;
nResult: Integer;
const
szFilePath =Application.EXEName+'\*.txt';
begin
//填充ListBox
ListBox1.Items.clear;
nResult:=FindFirst(szFilePath,faAnyFile,SearchRec); //查找第一个文件
while nResult = 0 do //如果返回值为0表示找到文件
begin
//将找到的文件添加到ListBox中;ExtractFilepath函数用于提取文件路径
ListBox1.Items.Add(Copy(SearchRec.Name,1,Length(SearchRec.Name)-POS('.',SearchRec.Name)+1));
nResult:=FindNext(SearchRec); //继续查找下一个文件,至到返回值不为0时
end;
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormShow(Sender: TObject);
var
SearchRec: TSearchRec;
nResult: Integer;
const
szFilePath =Application.EXEName+'\*.txt';
begin
//填充ListBox
ListBox1.Items.clear;
nResult:=FindFirst(szFilePath,faAnyFile,SearchRec); //查找第一个文件
while nResult = 0 do //如果返回值为0表示找到文件
begin
//将找到的文件添加到ListBox中;ExtractFilepath函数用于提取文件路径
ListBox1.Items.Add(Copy(SearchRec.Name,1,Length(SearchRec.Name)-POS('.',SearchRec.Name)+1));
nResult:=FindNext(SearchRec); //继续查找下一个文件,至到返回值不为0时
end;
end;end.
去掉Project后的.Exe就行了
StrDir:=GetCurrentDir;
db:=StrDir+'\*.ini';
db:=StrDir+'\*.txt';
const
szFilePath =Application.EXEName+'\*.txt';修改:
szFilePath :=ExtractFileDir(Application.EXEName)+'\*.txt';
var
SearchRec: TSearchRec;
s:string;
nResult: Integer;
//const
//szFilePath =ExtractFilePath(Application.ExeName));begin
//填充ListBox
s:=ExtractFilePath(Application.ExeName)+'\*.txt';
ListBox1.Items.clear;
nResult:=FindFirst(s,faAnyFile,SearchRec); //查找第一个文件
while nResult = 0 do //如果返回值为0表示找到文件
begin
//将找到的文件添加到ListBox中;ExtractFilepath函数用于提取文件路径
ListBox1.Items.Add(Copy(SearchRec.Name,1,Length(SearchRec.Name)-4));
nResult:=FindNext(SearchRec); //继续查找下一个文件,至到返回值不为0时
end;
end;