西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁(yè)編程開發(fā)其它知識(shí) → WP7&WP8開發(fā)、文件讀寫操作上的區(qū)別

WP7&WP8開發(fā)、文件讀寫操作上的區(qū)別

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:西西整理時(shí)間:2013/6/8 15:08:17字體大。A-A+

作者:西西點(diǎn)擊:34次評(píng)論:0次標(biāo)簽: 文件操作

  • 類型:游戲其他大。197KB語(yǔ)言:英文 評(píng)分:3.3
  • 標(biāo)簽:
立即下載

一、WP7的文件操作:

如果在wp7平臺(tái)上去寫入一個(gè)文件,我們會(huì)使用: IsolatedStorageFile

代碼如下:

①寫入文件
private void WriteFile(string fileName, string content)
{
using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.CreateFile(fileName))
{
using (StreamWriter streamWriter = new StreamWriter(isolatedStorageFileStream))
{
streamWriter.Write(content);
}
}
}
}
②讀取文件
private string ReadFile(string fileName)
{
string text;
using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.OpenFile(fileName, FileMode.Open))
{
using (StreamReader streamReader = new StreamReader(isolatedStorageFileStream))
{
text = streamReader.ReadToEnd();
}
}
}
return text;
}
二、WP8文件操作

wp8與win8的文件操作方式類似,如果你在win8下寫過文件操作,那么WP8自然熟悉,這需要用到2個(gè)接口:IStorageFile和 IStorageFolder, 可以看出,一個(gè)是對(duì)文件的操作,一個(gè)是對(duì)目錄的。

這兩個(gè)接口均在 :Windwos.Storage組件中,

注意:因在windows 8 開發(fā)中大量使用了WinRT異步編程方式,故在WP8中編程亦如此。

代碼如下:

①寫入文件:

public async Task WriteFile(string fileName, string text)
{
IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (Stream stream = await storageFile.OpenStreamForWriteAsync())
{
byte[] content = Encoding.UTF8.GetBytes(text);
await stream.WriteAsync(content, 0, content.Length);
}
}
②讀取文件:

public async Task<string> ReadFile(string fileName)
{
string text;
IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
IStorageFile storageFile = await applicationFolder.GetFileAsync(fileName);
IRandomAccessStream accessStream = await storageFile.OpenReadAsync();
using (Stream stream = accessStream.AsStreamForRead((int)accessStream.Size))
{
byte[] content = new byte[stream.Length];
await stream.ReadAsync(content, 0, (int) stream.Length);
text = Encoding.UTF8.GetString(content, 0, content.Length);
}
return text;
}
三、兼容性問題

上面的代碼分別對(duì)WP7與WP8平臺(tái)的文件寫入與讀取操作進(jìn)行了簡(jiǎn)單的說明

那么在WP7應(yīng)用中的文件目錄結(jié)構(gòu)定義,是不是在WP8中也是一樣的呢?

其實(shí),兩者是有點(diǎn)區(qū)別的,這點(diǎn)區(qū)別,也是日后WP7向WP8遷移過程中必須要考慮的元素之一。

在WP7中,創(chuàng)建文件test.txt,那么,它在隔離存儲(chǔ)區(qū)的文件目錄結(jié)構(gòu)如下:

C:\Data\Users\DefaultAppAccount\AppData\{ProductID}\Local\IsolatedStore\test.txt

在WP8中,創(chuàng)建相同文件,文件存儲(chǔ)目錄結(jié)構(gòu)如下:

C:\Data\Users\DefaultAppAccount\AppData\{ProductID}\Local\test.txt

兩者一對(duì)比,我們發(fā)現(xiàn):在WP8開發(fā)中,如果使用現(xiàn)有的WP7代碼時(shí),如果你不想改變?cè)形募夸浗Y(jié)構(gòu),那你就必須要首先獲取WP7隔離存儲(chǔ)目錄.即:

IStorageFolder applicationFolder =await ApplicationData.Current.LocalFolder.GetFolderAsync("IsolatedStore");
最后,想說明的是:

如果你想“偷懶”或者說減少開發(fā)工作量,就看看我上面說的這些

但如果打算做WP8/WIN8兩個(gè)平臺(tái),最好全新開發(fā)基于WP8平臺(tái)的應(yīng)用,這可以加快你的Win8應(yīng)用的移植。

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過審核才能顯示)