// 定义路径
#ifdef _DEBUG
_LIT(KPath, "c://System//Apps//myfile");//C盘对应/epoc32/wins/c
#else
_LIT(KPath, "e://myfile");// e盘对应手机上的e盘
#endif
// 用于保存文件夹名称
RPointerArray<HBufC> iFolderlist;
// 第一种方法
CDirScan* ds = CDirScan::NewLC(CCoeEnv::Static()->FsSession());
TRAPD(err,ds->SetScanDataL(KPath,KEntryAttNormal,ESortByName|EAscending,CDirScan::EScanDownTree));
if (err!=KErrNone)
{
CleanupStack::PopAndDestroy(ds);
return;
}
TPtrC str(gAppPath);
int nPathLen = str.Length();
TFileName fullname;
CDir* c = NULL;
while(1)
{
ds->NextL(c);
if (!c)
{
break;
}
fullname.Copy(ds->FullPath());
int nNameLen = fullname.Length() - nPathLen;
if(nNameLen > 0)
{
HBufC* foldername = HBufC::NewL(20);
*foldername = fullname.Mid(nPathLen, nNameLen-1);
iFolderlist.AppendL(foldername);
}
delete c;
c=NULL;
}
CleanupStack::PopAndDestroy(ds);
iFolderlist.ResetAndDestroy();
// 第二种方法
RFs Fs;
User::LeaveIfError(Fs.Connect());
CDir *dir = NULL;
// 获取KPath下的文件和文件夹列表
User::LeaveIfError(Fs.GetDir(KPath,KEntryAttNormal|KEntryAttMatchMask,ESortNone,dir));
for (TInt i=0; i<dir->Count(); i++)
{
// 如果是文件夹,就将文件夹名称保存到iFolderlist
if ((*dir)[i].IsDir())
{
HBufC* foldername = (*dir)[i].iName.AllocL();
iFolderlist.AppendL(foldername);
}
}
delete dir;
dir = NULL;
Fs.Close();
iFolderlist.ResetAndDestroy();
注:
第一种方法在执行ds->NextL(c);时非常耗时,本人用N6670手机试过,当KPath下有340个左右的文件和文件夹时,耗时大概是15秒,而用第二种方法不到1秒,所以建议使用第二种方法.
End.............