symbian文件和目录

 

下面我们集中探讨一下有关目录和文件的操作方式:
首先介绍一个类:TParse
用过的朋友都知道,这个类的主要功能就是处理路径,先看一段代码:
----------------------------------
_LIT(KPath,"c://new//meeting.wrd");
...
TParse p;
p.Set(KPath,NULL,NULL);
p.Name();//gives "meeting"
p.NameAndExt();//gives "meeting.wrd"
...
----------------------------------
通过上面的例子,对TPase的功能应该有了一定的了解,对了这个类就是用来处理有关路径的各种信息采集的。当然,它还有合并两个路径的功能,如下:
----------------------------------
_LIT(KSpec,"A:file1");
_LIT(KRelated,"c://path1//related.xxx");
TParse fp;
fp.Set(KSpec,&KRelated,NULL);
----------------------------------
上面操作的结果就是A:/path1/file1.xxx
提示:当我们需要提取指定路径的某些必要信息时,就需要我们使用TParse类来操作这个路径,从而达到目的,在处理路径时,这个类会经常使用!!

操作文件夹:
指定一个绝对路径
比如:指定_LIT(KBitmapFolder, "c://nokia//Images//Pictures//");
这个路径就可以代表手机存储图片文件的文件夹。
这种方式简单易用,而且在大多数nokia的手机上,往往路径都是一定的,所以这种方式是完全可行的。但是为了增强程序的可移植性,还是不要用绝对路径的好。因为我们有了第二种方法:
PathInfo类:
我们可以通过这个类获取当前设备的存储器路径。
例如:
根目录:
PathInfo::PhoneMemoryRootPath()
存储图片文件目录:
PathInfo::ImagesPath()
存储安装SIS文件目录:
PathInfo::InstallsPath()
存储声音文件目录:
PathInfo::SoundsPath()
如果想要定位MMC卡这种外加的存储器应该这样:
#include <PathInfo.h>
TFileName path = PathInfo::MemoryCardRootPath();
这种方式的效果更好,但是PathInfo这个类是在S60 2.0平台的,Symbian 6.1也就是S60 1.0平台却用不了!大家可以选择使用。

因为我们这篇文章涉及到的文件主要是图片和声音,因此只需要使用PathInfo::ImagesPath()和PathInfo::SoundsPath()来获得相应文件夹的地址。随后我们也就可以通过这个地址来操作相应的文件了。

操作文件:
Symbian OS下去操作文件主要是通过枚举所有特定文件夹下的文件以及查找指定的文件来完成的。
枚举某个文件夹下的文件:
-----------------------------------
TBuf<50> path,tpath;
TBuf<256> filename;
RFs iSessionRFs;
CDir* dirList;
// Number, name and file size
_LIT(KStringSize,"%S%S");
User::LeaveIfError(iSessionRFs.Connect());
path = PathInfo::PhoneMemoryRootPath();
path.Append(PathInfo::PicturesPath()); //picture的文件夹
User::LeaveIfError(iSessionRFs.GetDir(path, //读出文件夹下文件信息
KEntryAttMaskSupported,
ESortByName,
dirList));
TInt j = dirList->Count(); //文件数目
for (TInt i = 0;i<j;i++)
{ //添加你的操作
filename.Format(KStringSize,&path,&(*dirList)[0].iName);
CAknInformationNote* informationNote;
informationNote = new (ELeave) CAknInformationNote;
informationNote->ExecuteLD(filename);
}
delete dirList;
------------------------------------
通过上面的例程可以看出枚举文件夹中的文件主要是通过GetDir()函数来完成的,这个函数的介绍如下:
--------------------------------------------------------------------------------
GetDir()
TInt GetDir(const TDesC& aName,TUint anEntryAttMask,TUint anEntrySortKey, CDir*& anEntryList) const;
Description
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.

Notes:

If sorting by UID (ESortByUid is OR'ed with the entry sort key), then UID information will be included in the listing whether or not KEntryAttAllowUid is specified in anEntryAttMask.

The function sets anEntryList to NULL, then allocates memory for it before appending entries to the list. Therefore, anEntryList should have no memory allocated to it before this function is called, otherwise this memory will become orphaned.

The caller of this function is responsible for deleting anEntryList after the function has returned.

Parameters
const TDesC& aName Name of the directory for which a listing is required. Wildcards may be used to specify particular files.

TUint anEntryAttMask Bitmask indicating the attributes of interest. Only files and directories whose attributes match those specified here can be included in the listing. For more information see KEntryAttMatchMask and the other directory entry details. Also see KEntryAttNormal and the other file or directory attributes.

TUint anEntrySortKey Flag indicating the order in which the entries are to be sorted. This flag is defined in TEntryKey.

CDir*& anEntryList On return contains a list of directory and file entries.

Return value
TInt KErrNone if successful, otherwise another of the system-wide error codes.
--------------------------------------------------------------------------------
上面介绍摘自Series 60 2.1 help。
主要使用方式就是用户给定一个path,也就是第一个参数const TDesC& aName,通过调用,该文件夹中的内容就会返回到第四个参数CDir*& anEntryList中,接下来这个list中存储的内容就是该文件夹下的文件信息,至于中间两个参数可以设置一些屏蔽信息,使得用户可以取出自己需要的文件信息。

查找相应文件:
-------------------------------------
TFindFile file_finder(aSession); // 1
CDir* file_list; // 2
TInt err = file_finder.FindWildByDir(aWildName,aScanDir, file_list); // 3
while (err==KErrNone)
{
TInt i;
for (i=0; i<file_list->Count(); i++) // 4
{
TParse fullentry;
fullentry.Set((*file_list)[i].iName,& file_finder.File(),NULL); // 5,6,7
// Do something with the full Symbian OS filename
DoOneFile(aSession, fullentry.FullName()); // 8
}
delete file_list; // 9
err=file_finder.FindWild(file_list); // 10
}
-------------------------------------
具体操作
1. 生成一个TFindFile对象.
2. 在赋值前FindWildByDir()将file_list初始化,所以这时list并没有分配空间.
3. 开始查找指定文件. 注意aWildName和aScanDir的使用,aWildName一般是完整文件名(例如, *.gdr),aScanDir则是相应目录,并不需要一个具体的盘符(例如, /System/Fonts/).
4. list中文件数目.
5. (*file_list)[i].iName表示找到文件的完整名字(e.g. Eon.gdr).
6. file_finder.File()表示文件的具体盘符以及路径(for example Z:/System/Fonts/).
7. TParse::Set()可以完成合并.
8. TParse::FullName()返回该文件的盘符、路径及完整名字.函数DoOneFile()可以是用户自己定义做相应的操作, (因为DoOneFile() 需要访问文件服务器, 所以RFs对象必须作为参数传递).
9. FindWildByDir()和FindWildByPath()都对CDir的对象分配了相应的空间,因此使用完必须删除CDir的对象.
10. 最后,你还可以使用TFindFile::FildWild()继续查询下一个盘符.
--------------------------------------------------------------------------------

有了上面的介绍,你一定对Symbian OS 中的操作有了一定的了解,更多更详细的使用方法还是需要你自己阅读Symbian OS sdk相应的帮助文件。以上给出的例程在vc++ 6.0/s60 sdk 2.1下调试通过。


文章来源:http://symbian.org.cn/bbs/viewtopic.php?t=883

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值