现在的位置: 首页 > 综合 > 正文

Windows Phone 7 隔离存储空间及文件操作

2012年02月25日 ⁄ 综合 ⁄ 共 4005字 ⁄ 字号 评论关闭

Windows Phone 7的隔离存储空间

  1. 概念:

Windows Phone 7中所有的文件IO操作被限制在隔离存储空间里面,在隔离存储空间里面可以增加,删除和修改目录文件,在隔离存储空间里面可以存储程序的配置信息,但是每个应用程序的隔离存储空间都是独立的,相当于Windows Phone 的一块内存被单独划出来了,只有这一块的内部(应用程序本身)才可以访问其内部的信息,而外部(其他的应用程序)无法访问。

      2.  目录操作

两个重要的类:

IsolatedStorageFile:用于操作隔离存储空间里面的目录以及文件,例如增,删,改,查等。

IsolatedStorageFileStream:用于读写操控隔离存储空间里面的文件流,例如当我们需要往某个文件写入东西的时候便会使用到这个类。

IsolatedStorageSettings:用于存储程序的配置信息的Dictionary,例如应用程序的一些Key和Value等。

       3.  使用隔离存储空间需要引用两个命名空间:

using System.IO.IsolatedStorage;
using System.IO;

对隔离存储空间的操作和传统的文件IO操作类似

在隔离存储空间里里面没有绝对路径或则说没有根目录,通常来说例如在我们的Windows 电脑上,一个文件夹它的根目录在C盘或则D盘或则E盘等等,但是在Windows Phone 7中则没有根目录,因此也就没有绝对路径。所以我们要取得应用程序的隔离存储空间不能通过路径来获得,只能通过GetUserStoreForApplication()方法来获得。

     

 示例演示:

 using System.IO.IsolatedStorage;
using System.IO;
        private const string FolderName = "temp1";//定义一个常量,必须在此初始化
        private void newbutton_Click(object sender, RoutedEventArgs e)
        {
           using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
          //使用using表示这个类使用完之后可以自动的释放资源,即调用Dispose()方法。
//获得应用程序的隔离存储空间
            { 
            file.CreateDirectory(FolderName);//新建一个文件夹
            MessageBox.Show("新建成功!");
            }
        }

        private void Checkbutton_Click(object sender, RoutedEventArgs e)
        {
            using (IsolatedStorageFile file=IsolatedStorageFile.GetUserStoreForApplication())
            {
            if(file.DirectoryExists(FolderName))//目录存在
            {
            MessageBox.Show(FolderName+"已经存在!");
            }
            else
            {
            MessageBox.Show(FolderName+"不存在!");
            }
          }
        }

        private void Deletebutton_Click(object sender, RoutedEventArgs e)
        {
            using (IsolatedStorageFile file=IsolatedStorageFile.GetUserStoreForApplication())
            {
            if(file.DirectoryExists(FolderName))
            {
               file.DeleteDirectory(FolderName);//删除目录
               MessageBox.Show(FolderName + "文件已删除");
            }
            else
            {
            MessageBox.Show("没有可删除的文件");
            }
           }
        }

    4.   文件操作

          文件操作和目录的操作类似,也包括增,删,改,查等操作.

          a. 新建文件操作

              

 using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                FileStream filestream = file.CreateFile(FileName);//新建一个文件
                filestream.Close();//关闭文件流,若不关闭,则删除操作出现异常
//这里由于CreateFile()返回的是一个文件流,即FileStream,因此我们声明一个FileStream
//变量来保存这个文件
            }

            b. 删除文价操作

 using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (file.FileExists(FileName)) //检查文件
                {
                    file.DeleteFile(FileName);
                }
            }

           c. 文件信息的写入和读取(请看下面代码)

  private void Writebutton_Click(object sender, RoutedEventArgs e)
        {
//获得应用程序的隔离存储空间
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
//打开文件流,FileMode.OpenOrCreate表示如果文件不存在则新建文件,
//FileAccess.Write表示是写入操作
                using (IsolatedStorageFileStream filestream = file.OpenFile(FileName, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        StreamWriter strw = new StreamWriter(filestream);//指定往我们打开的文件流中写入信息
                        strw.WriteLine(MsgtextBox.Text);//将MsgtextBox中的Text值写入文件中
                        strw.Close();//关闭文件写入
                }
            }
        }

        private void Readbutton_Click(object sender, RoutedEventArgs e)
        {
//获得应用程序的隔离存储空间
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
               {
                if (file.FileExists(FileName))//如果文件存在才可进行读取
                {
//打开要读取的文件流,FileMode.Open表示只打开文件,
//FileAccess.Read表示是进行读取操作
                    using (IsolatedStorageFileStream filestream = file.OpenFile(FileName, FileMode.Open, FileAccess.Read))
                    {
                        StreamReader strr = new StreamReader(filestream);//指定向打开的文件流读取信息
                        MsgtextBox.Text = strr.ReadToEnd();//读取文件的全部信息
                        strr.Close();//关闭读取操作
                    }
                }
            }
        }

读取及写入文件小结:

      首先我们需要获得应用程序的隔离存储空间,然后我们需要打开指定的文件(打开之后是以文件流的形式返回),在打开文件的过程中有几种情况并通过FileMode来确定我们要以那种方式打开。接着便可以对其文件进行相应的操作,通过FileAccess来确定我们是想进行读取还是写入的操作。

   5.  隔离存储空间的配置信息

        a. 往隔离存储空间的配置信息中写入信息

 private void SettingsWritebutton_Click(object sender, RoutedEventArgs e)
        {
            //将Settings放入ApplicationSettings的字典中,并将SettingstextBox的值赋给Settings
            IsolatedStorageSettings.ApplicationSettings[Settings] = SettingstextBox.Text;
            IsolatedStorageSettings.ApplicationSettings.Save();//保存信息,这一步非常关键,如果不保存,那么我们刚才添加的Settings信息就不会保存到应用程序的隔离存储空间中,而是放在了内存中,因此将无法读取Settings。
        }

        b. 从隔离存储空间的配置信息中读取信息

 private void SettingsReadbutton_Click(object sender, RoutedEventArgs e)
        {
            //判断字典中是否有Settings这个Key,如果有则将其值赋给SettingstextBox
            if (IsolatedStorageSettings.ApplicationSettings.Contains(Settings))
            {
                //由于字典中存放的是对象,取出来之后需要转换为相应的数据类型。
        SettingstextBox.Text = IsolatedStorageSettings.ApplicationSettings[Settings] as string;
            }
        }

 说明:以上内容参考Jake Lin的视频而写,不懂可参考Jake Lin的视频,值得大家一看。

(版权所有,转载请标明出处)

抱歉!评论已关闭.