Windows Phone 7独立存储空间IsolatedStorage

Windows Phone 7的solatedStorage可以用来保存应用程序的数据和设置。结构图如下

 

一、相关类

  1.IsolatedStorageFile类

    1)描述:表示在独立存储空间中的文件和目录

    2)重要属性

      long AvailableFreeSpace:IsolatedStorage有效的剩余空间。

      long Quota:IsolatedStorage的总容量。

    3)重要方法

      void CreateDirectory(string dir):在IsolatedStorage中创建一个指定的目录。

      IsolatedStorageFileStream CreateFile(string path):在IsolatedStorage中创建一个文件。

      void DeleteDirectory(string dir):删除IsolatedStorage的指定目录。

      void DeleteFile(string file):删除IsolatedStorage的指定文件。

      bool DirectoryExists(string path):判断IsolatedStorage中是否存在指定的目录,如果存在则返回true。

       bool FileExists(string path):判断IsolatedStorage中是否存在指定的文件,如果存在则返回true。

      string[] GetDirectoryNames():获取IsolatedStorage中的所有目录名称。

      string[] GetFileNames():获取IsolatedStorage中的所有文件名称。

      static IsolatedStorageFile GetUserStoreForApplication():由一个应用程序调用,获得用户范围可以使用的独立存储。

      IsolatedStorageFileStream OpenFile(string path, FileMode mode):打开一个指定的文件。

  2.IsolatedStorageFileStream类

    1)描述:打开一个文件流。

二、应用

  1.创建并写入文件

    首先我们获取空间

    var appStorage = IsolatedStorageFile.GetUserStoreForApplication();

    保存的文件名
            string fileName = "simple.txt";

    打开流
            using (var file=appStorage.OpenFile(fileName,System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.Write))
            {
                using (var writer=new System.IO.StreamWriter(file))
                {

      写入数据
                    writer.Write(this.inputInfo.Text);
                }
            }  

    2.打开并读取文件

      using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (StreamReader reader=new StreamReader(store.OpenFile("simple.txt",FileMode.Open,FileAccess.Read)))
                {
                    inputInfo.Text = reader.ReadToEnd();
                }
            } 

 

三、源码下载

    点击这里下载示例源码

posted @ 2010-12-28 19:28  ForrestWoo  阅读(1437)  评论(2编辑  收藏  举报