[code=C/C++][/code]
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{ }
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{ ShowMessage( BrowDir("c:\\","services.isc"));
} //---------------------------------------------------------------------------
String __fastcall TForm1::BrowDir(String path,String file)
{ TSearchRec sr;
static String dpath; //关键是这里,必须定义为静态,否则下一次递归将丢掉所得到的值
if (FindFirst(path+"*.*", faAnyFile, sr) == 0) //列举所有的目录
{
do
{
if((sr.Attr & faDirectory) && sr.Name!="." && sr.Name!="..") //判断是否是目录,并排除目录“.”和“..”
BrowDir(path+sr.Name+"\\",file); //调用函数本身,进入子目录
else
if( sr.Name.UpperCase()==file.UpperCase())//找到所需文件
return dpath=path;
} while (FindNext(sr) == 0);
FindClose(sr);
}
return dpath;
}
//---------------------------------------------------------------------------
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{ }
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{ ShowMessage( BrowDir("c:\\","services.isc"));
} //---------------------------------------------------------------------------
String __fastcall TForm1::BrowDir(String path,String file)
{ TSearchRec sr;
static String dpath; //关键是这里,必须定义为静态,否则下一次递归将丢掉所得到的值
if (FindFirst(path+"*.*", faAnyFile, sr) == 0) //列举所有的目录
{
do
{
if((sr.Attr & faDirectory) && sr.Name!="." && sr.Name!="..") //判断是否是目录,并排除目录“.”和“..”
BrowDir(path+sr.Name+"\\",file); //调用函数本身,进入子目录
else
if( sr.Name.UpperCase()==file.UpperCase())//找到所需文件
return dpath=path;
} while (FindNext(sr) == 0);
FindClose(sr);
}
return dpath;
}
//---------------------------------------------------------------------------