一起學Windows Phone 7開發(六.Isolate Storage)

來源:互聯網
上載者:User

windows phone 7 目前版本上已確定沒有檔案系統(也就是說filestream、OpenFileDialog這樣的功能都是不能使用了)和資料庫系統了,那在開發時需要儲存一些使用者配置資訊或臨時資料在本地怎麼辦? 答案是只能使用silverlight的特色功能Isolate Storage來儲存文字檔、XML檔案或INI檔案的方式來替代了。

其實使用Isolate Storage的最大好處就是安全性了,因為只有本程式可以訪問該地區,而其他程式是無法訪問的。這樣也就可以對一此敏感性資料的儲存不用自已再加密了。但是這個地區是有限的(預設為2GB),不能夠儲存很大的資料,以及長期儲存資料。如果您非要儲存大量資料,以及長期儲存的話,目前只能儲存在雲端而不是本地。

       對於Isolate Storage的使用,其實也很簡單,和FileStream基本上是一樣的。下面就是一些基本操作的代碼:

1. 開啟Isolate Storage:

首先引入命名空間System.IO.IsolatedStorage;

開啟storage IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

2. 建立檔案夾:

 isf.CreateDirectory("MyFolder");

3. 建立檔案:

StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.OpenOrCreate, isf));

writer.WriteLine(“Hello World!”);

writer.Close();

 

4. 讀取檔案:

StreamReader reader = new StreamReader(new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.Open, isf));

string text = reader.ReadLine();

 

5. 刪除檔案:

isf.DeleteFile("MyFolder\\myFile.txt");

6. 刪除檔案夾:

isf.DeleteDirectory("MyFolder");

7. 判斷檔案或檔案夾是否存在:

isf.FileExit("MyFolder\\myFile.txt");

isf.DirectoryExit("MyFolder");

8.也可以使用IsolateStorageFileStream來操作檔案或檔案夾,用法和FileStream 基本相同.

相關的API詳細資料可以查看

http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage(VS.95).aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.