RFs 目录操作

RFs 目录操作

RFs 可以通过 Connect() 连接到文件服务器,完毕后通过 Close() 关闭连接

常用操作:

  1. MkDir 与 MkDirAll 创建目录
  2. GetDir 可以得到 目录的列表到一个 CDir 对像中,这个CDir保存每个一目录的信息,用 TEntry 保存
  3. 通过 TEntry 得到每个目录的信息
  4. 注意的是对于目录的结尾一定加上 “\\”,否则认为不是一个目录
  5. SessionPath 会得到当前会话的路径,SetSessionPath 设置路径
  6. 头文件加上 f32file.h ,mmp 文件的 library 中添加上 efsrv.lib,然后重新编译

下面是练习用到的代码

代码
#include  < f32file.h >

//   Constants
_LIT(KTextConsoleTitle,  " Console " );
_LIT(KTextFailed, 
"  failed, leave code = %d " );
_LIT(KTextPressAnyKey, 
"  [press any key]\n " );

//   Global Variables
LOCAL_D CConsoleBase *  console;   //  write all messages to this
LOCAL_D RFs iFs;
void  printDriveInfo()
{
 
//  连接文件服务器
 User::LeaveIfError(iFs.Connect());
 _LIT(KEnter,
" \n " );
 _LIT(KPromp,
" There are drives list:\n " );
 _LIT(KChar,
" %c\n " );
 console
-> Printf(KPromp);
 TChar DriveLetter;
 TDriveList dList;
 iFs.DriveList(dList);
 
for (TInt i = 0 ;i < dList.Length();i ++ )
 {
  
if  (dList[i])
  {
   iFs.DriveToChar(i,DriveLetter);
   console
-> Printf(KChar,DriveLetter);
   console
-> Printf(KEnter);
  }
 }

 
//  用完后,关闭连接
 iFs.Close();
}
void  printDirInfo( const  TEntry & );
void  viewDirInfo()
{
 User::LeaveIfError(iFs.Connect());
 _LIT(KMyDir,
" c:\\myfile\\ " );
 _LIT(KmyDirAll,
" c:\\mytxtfile\\txt\\ " );
    _LIT(KretInfo,
" return code=%d " );
 TInt iRet;
 iRet 
=  iFs.MkDir(KMyDir);
 console
-> Printf(KretInfo,iRet);
 iRet 
=  iFs.MkDirAll(KmyDirAll);
 console
-> Printf(KretInfo,iRet);
 TBuf
< 20 >  sessionPath;
 User::LeaveIfError(iFs.SessionPath(sessionPath));
 _LIT(K1,
" session path is: " );
 _LIT(KEnter,
" \n " );
 console
-> Printf(K1);
 console
-> Printf(sessionPath);
 console
-> Printf(KEnter);
 TBuf
< 20 >  defaultSessionPath;
 User::LeaveIfError(iFs.DefaultPath(defaultSessionPath));
 _LIT(K2,
" default path: " );
 console
-> Printf(K2);
 console
-> Printf(defaultSessionPath);
 console
-> Printf(KEnter);
 _LIT(KNewPath,
" c:\\myfile\\ " );
 User::LeaveIfError(iFs.SetSessionPath(KNewPath));
 User::LeaveIfError(iFs.SessionPath(sessionPath));
 console
-> Printf(K1);
 console
-> Printf(sessionPath);
 console
-> Printf(KEnter);
 
    _LIT(KDir,
" C:\\mytxtfile\\ " );
 CDir
*  dir  =  NULL;
 console
-> ClearScreen();
 User::LeaveIfError(iFs.GetDir(KDir,KEntryAttNormal
| KEntryAttMatchMask,ESortNone,dir));
 
for (TInt i = 0 ;i < dir -> Count();i ++ )
 {
  printDirInfo((
* dir)[i]);
  console
-> Getch();
  console
-> ClearScreen();
  console
-> Printf(KEnter);
 }
 
 iFs.Close();
}
void  printDirInfo( const  TEntry &  aEntry)
{
 _LIT(Kentery,
" \n " );
 _LIT(KIsDir,
" directory\n " );
 _LIT(KIsReadOnly,
" Readonly\n " );
 _LIT(KIsHidden,
" Hidden\n " );
 _LIT(KIsSystem,
" System\n " );
 _LIT(KIsArchive,
" Archive\n " );
 console
-> Printf(aEntry.iName);
 console
-> Printf(Kentery);
 TBuf
< 30 >  dateString;
 _LIT(KDatestring4,
" %Y%M%D%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S\n " );
 aEntry.iModified.FormatL(dateString,KDatestring4);
 console
-> Printf(dateString);
 _LIT(KFmt1,
" size:%d bytes\n " );
 console
-> Printf(KFmt1,aEntry.iSize);
 
if  (aEntry.IsDir())
  console
-> Printf(KIsDir);
 
if  (aEntry.IsReadOnly())
  console
-> Printf(KIsReadOnly);
 
if  (aEntry.IsHidden())
  console
-> Printf(KIsHidden);
 
if  (aEntry.IsSystem())
  console
-> Printf(KIsSystem);
 
if  (aEntry.IsArchive())
  console
-> Printf(KIsArchive);
}
//   Local Functions
LOCAL_C  void  MainL( const  TDesC &  aArgs)
    {
    
//
    
//  add your program code here, example code below
    
//
    
// console->Write(_L("Hello, world!\n"));
 
// printDriveInfo();
 viewDirInfo();
    console
-> Printf(_L( " Command line args: \ " % S\ " \n " ),  & aArgs);
    }

 



安平2009@原创
[email protected]

你可能感兴趣的:(目录)