symbian 获取指定路径下的文件夹列表


// 定义路径
#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.............
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值