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下調試通過。


本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/gengyan_99/archive/2010/08/29/5848036.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章