Windows Phone学习笔记(5) — — 存储文件

  有几天的时间没有学习和总结WP了,今天学习在WP中的文件夹和文件的存储。在WP中存储数据都要用到内存,想要操作手机的内存存储,就要用到IsolatedStorageFile类,IsolatedStorageFile类表示一个独立的存储区,包含的文件和目录。IsolatedStorageFileStream类公开独立存储中的文件。

  操作独立存储的步骤如下:

  • 获取应用程序的虚拟存储
  • 创建父文件夹
  • 创建文本并将其添加到独立存储文件
  • 读取放置在存储文件中的文本

首先获取应用程序的虚拟内存:IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();

创建父级文件夹:myStore.CreateDirectory("MyFolder");

创建文本并将其添加到独立存储文件:

    // 指定文件路径和选项。 (var isoFileStream = new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.OpenOrCreate, myStore))
    {
        //写入数据 (var isoFileWriter = new StreamWriter(isoFileStream))
        {
            isoFileWriter.WriteLine(txtWrite.Text);
        }
    }
读取存储中的文本:
        // 指定而文件路径和选项using (var isoFileStream = new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.Open, myStore))
        {
            // 读取数据using (var isoFileReader = new StreamReader(isoFileStream))
            {
                txtRead.Text = isoFileReader.ReadLine();
            }
        }

页面只用到了两个按钮和一个Text。

删除文件:myStore.DeleteFile("MyFolder\\myFile.txt");

其他信息请参考:http://msdn.microsoft.com/zh-cn/library/ff626519(v=vs.92).aspx

        IsolatedStorageFile ClassIsolatedStorageFileStream Class

原文地址:https://www.cnblogs.com/renhao0118/p/2756689.html