文件/ 目录/ 驱动器

    

在Symbian OS中,Flash(闪存)通常被定义为C盘。另外,在Symbian智能手机中还有一个ROM存储器,通常被映射为Z盘,用户的许多文件存放在Z盘中,其他可移动存储器(如存储卡等)则映射为d,e等盘符。
一个完整的Symbian OS文件名包括以下四部分:
1. 驱动器名,即盘符。Symbian以\symbian\8.1a\S60_2nd_FP3\作为根目录的起点。
2. 路径。
3. 文件名。
4. 文件扩展名。
一个完整的文件名不能超过255个字符。Symbian OS中提供了TFileName类来存储文件名。
const TInt KMaxFileName=0x100;
typedef TBuf<KMaxFileName> TFileName;

应用程序要对文件进行读写,必须使用文件服务器提供的各种读写函数。在使用这些函数前,必须建立应用程序与文件服务器的连接,这种连接称为会话(session)。Symbian OS提供了RFs类,该类的Connect()/Close()函数用来建立和关闭连接。应用程序和文件服务器建立连接的具体过程如下:
1. 定义一个RFs对象iFs。
2. 调用Connect()建立连接 User::LeaveIfError(iFs.Connect());
3. 当文件读写完成后,调用iFs.Close()关闭连接,结束会话并释放相关资源。

常用的驱动器操作:
首先,熟悉一下一些结构/类的定义
const TInt KMaxDrives=26;
typedef TBuf8<KMaxDrives> TDriveList;
const TInt KMaxDriveName=0x02;
typedef TBuf<KMaxDriveName> TDriveName;

enum TDriveNumber
{
          EDriveA,       EDriveB,       EDriveC,       EDriveD,       EDriveE,
          EDriveF,       EDriveG,       EDriveH,       EDriveI,       EDriveJ,
          EDriveK,       EDriveL,       EDriveM,       EDriveN,       EDriveO,
          EDriveP,       EDriveQ,       EDriveR,       EDriveS,       EDriveT,
          EDriveU,       EDriveV,       EDriveW,       EDriveX,       EDriveY,
          EDriveZ
};

enum TEntryKey
{
          ESortNone=0,ESortByName,ESortByExt,ESortBySize,ESortByDate,ESortByUid,
          EDirsAnyOrder=0,EDirsFirst=0x100,EDirsLast=0x200,
          EAscending=0,EDescending=0x400,EDirDescending=0x800
};

enum TFileMode
{
          EFileShareExclusive,EFileShareReadersOnly,EFileShareAny,
          EFileStream=0,EFileStreamText=0x100,
          EFileRead=0,EFileWrite=0x200
};

class TDriveInfo
{
public:
          TMediaType iType; // Whether the drive supports a battery and if so, its state
          TBatteryState iBattery; // The drive attributes. For more information see KDriveAttLocal and other drive attributes.

          /* The attributes of the media mounted on the drive. Replaced with: For more information see KMediaAttVariableSize and other media attributes.*/
          TUint iDriveAtt;
          TUint iMediaAtt; // The type of media mounted on the drive.
};

class TVolumeInfo
{
public:
           IMPORT_C TVolumeInfo();
          TDriveInfo iDrive;
          TUint iUniqueID;
          TInt64 iSize;
          TInt64 iFree;
          TBufC<KMaxFileName> iName;
};

RFs中与驱动器操作相关的方法:
TInt DriveList(TDriveList& aList) const;
Gets a list of the available drives. The drive list consists of an array of 26 bytes. Array index zero corresponds to drive A, one equals B etc. Each byte with a non zero value signifies that the corresponding drive contains recognised media.
TInt Drive(TDriveInfo& anInfo,TInt aDrive=KDefaultDrive) const;
Gets information about a drive and the medium mounted on it.
TInt Volume(TVolumeInfo& aVol,TInt aDrive=KDefaultDrive) const;
Gets volume information for a formatted device. This function provides additional information to that given by Drive(), including the volume label, if set, and the amount of free space on the disk
TInt GetDriveName(TInt aDrive,TDes& aDriveName) const;
Gets the name of a drive. Drive naming is optional. If the drive specified has not been assigned a name, this function returns a descriptor whose length is zero.
TInt SetDriveName(TInt aDrive,const TDesC& aDriveName);
Sets the name of a drive. Drive naming is optional. Any drive may be assigned a name and more than one drive may share the same name.
TInt SetVolumeLabel(const TDesC& aName,TInt aDrive=KDefaultDrive);
Sets the label for a volume.
  static TBool IsValidDrive(TInt aDrive);
Tests whether a drive number is valid. A valid drive number is any number between 0 and 25 inclusive.
static TInt CharToDrive(TChar aChar,TInt& aDrive);
Maps a drive character to the corresponding number. The drive character must be in the range A to Z. For example, drive A corresponds to zero, drive B corresponds to 1. For the drive number enumeration, see TDriveNumber.
static TInt DriveToChar(TInt aDrive,TChar& aChar);
Maps a drive number to the corresponding character. The drive number must be in the range 0 to 25. For example, drive number zero (EDriveA) corresponds to drive A, one (EDriveB) corresponds to drive B. For the drive number enumeration, see TDriveNumber.
TInt LockDrive(TInt aDrv, const TMediaPassword& aOld, const TMediaPassword& aNew, TBool aStr);
Locks the card in the specified drive.The function locks an unlocked card and sets the password. An existing password can be changed.The new password must be added to the controller's password store so that the controller can subsequently issue the password without the user having to be prompted for it again.
TInt UnlockDrive(TInt aDrv, const TMediaPassword& Pswd, TBool aStr);
Unlocks the card in the specified drive.The password must be added to the controller's password store so that the controller can subsequently issue the password without the user having to be prompted for it again.

目录操作主要包括建立一个目录或目录结构、访问目录的属性等。在进行目录操作之前仍然先与文件服务器进行连接。

RFs中常用的文件/目录操作方法如下:

TInt MkDir(const TDesC& aPath);

Makes a directory. It should be a sub-directory of an existing directory and its name should be unique within its parent directory, otherwise the function returns an error.

TInt MkDirAll(const TDesC& aPath);

Makes one or more directories. Any valid path component specified in the specified path which does not already exist is created as a directory.

TInt RmDir(const TDesC& aPath);

Note that if a filename is specified in the argument, it is ignored. Therefore, there should be a trailing backslash after the final directory name in the argument to indicate that it is a directory, not a filename.

TInt Delete(const TDesC& aName);

Deletes a single file. Wildcards are not allowed in either the file name or the extension, otherwise an error is returned.

TInt Rename(const TDesC& anOldName,const TDesC& aNewName);

Renames a single file or directory. It can also be used to move a file or directory by specifying different destination and source directories. If so, the destination and source directories must be on the same drive. If a directory is moved, then the directory structure beneath it is also moved.

If a directory specified by aNewName is different from one specified by anOldName, then the file or directory is moved to the new directory, see example below. The file or directory may not be moved to another device by this means, either explicitly (by another drive specified in the name) or implicitly (because the directory has been mapped to another device with SetSubst()).

TInt Replace(const TDesC& anOldName,const TDesC& aNewName);

Replaces a single file with another. This function does not support the use of wildcards. Unlike Rename(), it only applies to files.

TInt Att(const TDesC& aName,TUint& aAttValue) const;

Returns a file's attributes.

TInt SetAtt(const TDesC& aName,TUint aSetAttMask,TUint aClearAttMask);

Sets or clears the attributes of a single file.The function uses two bitmasks. The first bitmask specifies the attributes to set. The second specifies the attributes to clear.

TInt Modified(const TDesC& aName,TTime& aTime) const;

Gets the last modification date and time of a file or directory. If there has been no modification, the function gets the date and time of the file or directory's creation.

TInt Entry(const TDesC& aName,TEntry& anEntry) const;

Gets the entry details, including UID information, for a file or directory.

TInt SetModified(const TDesC& aName,const TTime& aTime);

Sets the date and time that the contents of a file or directory were modified.

TInt SetEntry(const TDesC& aName,const TTime& aTime,TUint aSetAttMask,TUint aClearAttMask);

Sets both the attributes and the last modified date and time for a file or directory.The function uses two bitmasks. The first bitmask determines which attributes should be set. The second bitmask determines which are cleared.

TInt RealName(const TDesC& aName,TDes& aResult) const;

Gets the real name of a file.This is used in circumstances where a file system needs to mangle Symbian OS natural names so that it can store them on that file system.

TInt GetDir(const TDesC& aName,TUint anEntryAttMask,TUint anEntrySortKey, CDir*& anEntryList) const;

Gets a filtered list of a directory's contents. The bitmask determines which file and directory entry types should be listed. The sort key determines the order in which they are listed.

TInt GetDir(const TDesC& aName,TUint anEntryAttMask,TUint anEntrySortKey, CDir*& anEntryList,CDir*& aDirList) const;

Gets a filtered list of the directory and file entries contained in a directory and a list of the directory entries only. The bitmask determines which file and directory entry types should be listed in anEntryList. The contents of the second list, aDirList are not affected by the bitmask; it returns all directory entries contained in directory aName. The sort key determines the order in which both lists are sorted.

TInt GetDir(const TDesC& aName,const TUidType& anEntryUid,TUint anEntrySortKey, CDir*& aFileList) const;

Gets a filtered list of a directory's contents by UID.The TUidType parameter determines which file entry types should be listed. The sort key determines the order in which they are listed.

TInt DefaultPath(TDes& aPath) const;

Gets the system default path. This path is assigned as the session path to new clients when they connect to the file server. There is a single system default path rather than one for each drive.

TInt SetDefaultPath(const TDesC& aPath);

Sets the system default path. This path is assigned as the session path to new clients when they connect to the file server. The session paths of existing clients remain unchanged. There is a single system default path rather than one for each drive.

TInt SessionPath(TDes& aPath) const;

Gets the session path. When a client connects to the file server, its session path is initialised to the system default path. The session path of an existing client can only be changed by SetSessionPath().

TInt SetSessionPath(const TDesC& aPath);

Sets the session path for the current file server client. When the client first connects to the file server, its session path is initialised to the system default path.

郑重声明:资讯 【文件/ 目录/ 驱动器】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——