×
Namespaces

Variants
Actions

如何列出文件和目录

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
翻译:
hoolee
最后由 hamishwillee 在 29 Aug 2012 编辑

下列代码片段演示了如何获得给定目录下(如C:\,D:\)文件的列表以及目录的列表

FileBrowser示例中的CRFsEngine类需要rfsengine.h中的相关描述,你可以在第三版FP1 SDK中找到。

#include <e32base.h>
#include <e32cons.h>
#include <e32debug.h>
#include <f32file.h>
#include "rfsengine.h"
 
_LIT(KFileBrowseTest, "FileBrowser");
_LIT(KCDrive, "C:\\");
_LIT(KPermissionDenied, "Permission Denied");
_LIT(KBackSlash, "\\");
 
 
GLDEF_C TInt E32Main()
{
// Create the cleanup stack
CTrapCleanup* cleanup = CTrapCleanup::New();
TRAPD(result, CallBrowseDirsL());
// Destroy the cleanup stack
delete cleanup;
 
return 0;
}
 
LOCAL_C void CallBrowseDirsL()
{
console = Console::NewL(KFileBrowseTest, TSize(KConsFullScreen, KConsFullScreen));
CleanupStack::PushL(console);
 
ListDirectoriesL(KCDrive);
 
CleanupStack::PopAndDestroy(); // close console
}
// Recursive function to list files and directories
LOCAL_C void ListDirectoriesL(const TDesC& aPath)
{
CRFsEngine* engine = CRFsEngine::NewL();
CleanupStack::PushL(engine);
 
RDebug::Print(aPath);
 
TInt result = engine->GetDirectoryAndFileList(aPath);
 
if (result==KErrNone)
{
TInt dirCount = engine->DirectoryCount();
TInt index = 0;
 
for (index=0; index<dirCount; index++)
{// Get directories
HBufC* subDirPath = SubDirPathLC(aPath, engine->DirectoryName(index));
ListDirectoriesL(*subDirPath);
CleanupStack::PopAndDestroy(subDirPath);
}
 
// List files in the directory
TInt fileCount = engine->FileCount();
index = 0;
for (; index<fileCount; index++)
{
RDebug::Print(engine->FileName(index));
}
}
else if (result==KErrPermissionDenied)
{
RDebug::Print(KPermissionDenied);
}
else
User::Leave(result);
 
CleanupStack::PopAndDestroy(engine);
}
 
 
// Allocates a heap buffer from aPath and aSubdir, with backslash terminator
LOCAL_C HBufC* SubDirPathLC(const TDesC& aPath, const TDesC& aSubDir)
{
HBufC* subDirBuf = HBufC::NewLC(aPath.Length() + aSubDir.Length() +1);
TPtr subDirPtr(subDirBuf->Des());
subDirPtr.Copy(aPath);
subDirPtr.Append(aSubDir);
subDirPtr.Append(KBackSlash);
return (subDirBuf);
}
This page was last modified on 29 August 2012, at 06:36.
52 page views in the last 30 days.