【WP之一】]独立存储

介绍:

提供一个磁盘存储空间,他是一种虚拟的文件系统,能存储小量的数据;在默认的情况下,它只能存储1MB的文件。根据使用方式及功能的不同,独立存储空间又包含两部分:独立设置存储和独立文件存储。除非卸载应用,否则数据不会消失。

第一是通过库中的键/值对,叫做IsolatedStorageSettings(独立设置存储),第二是通过创建真实的文件和目录,叫做IsolatedStorageFile(独立文件存储)

独立设置存储:
命名空间为:System.IO.IsolatedStorage;主要涉及System.IO.IsolatedStorage.IsolatedStorageSettings类
常用操作:
//创建操作独立设置存储必须的IsolatedStorageSettings类的对象
             IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        //
             settings.Add(key,value);    
         //
             settings.Remove("kk");
        //
             settings["kk"] = value;
        //
             string kk = (string)settings["kk"];
        
        //判断该键是否存在
             settings.Contains("kk");
        //清除
             settings.Clear();
        //最终都需要保存
             settings.Save();

独立文件存储:

命名空间为:System.IO.IsolatedStorage;主要涉及System.IO.IsolatedStorage.IsolatedStorageFile类。实际上,IsolatedStorage.IsolatedStorageFile类是 FileStream类 的一个子类。

         CreateDirectory()        创建一个新的独立存储文件夹 
         DeleteDirectory()        删除独立存储文件夹        
         CreateFile()                创建文件 
         DeleteFile()                删除文件                   
         GetFileNames()           得到文件名称集合 
         GetDirectoryName()    得到文件夹名称集合 
         OpenFile()                  打开文件 
         Remove()                  移除所有的文件和文件夹

常用操作:

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

 namespace PhoneApp19
 {
     public partial class MainPage : PhoneApplicationPage
     {
         //为程序获取一个虚拟的本地存储
         IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication();
         // 构造函数
         public MainPage()
         {
             InitializeComponent();
         }
         //写入数据
         private void btnWrite_Click(object sender, RoutedEventArgs e)
         {
             string filePath = txbFilePath.Text.Trim();
             string fileName = txbFileName.Text.Trim();
             string fullFileName = System.IO.Path.Combine(filePath,fileName);
             string content = txbContent.Text;
             //判断文件夹是否存在,若不存在则创建
             if (!storageFile.DirectoryExists(filePath))
             {
                 storageFile.CreateDirectory(filePath);
             }
             //写入
             using (StreamWriter writer = new StreamWriter(storageFile.OpenFile(fullFileName, FileMode.Append)))
             {
                 writer.WriteLine(content);
             }
         }
         //读取数据
         private void btnRead_Click(object sender, RoutedEventArgs e)
         {
             string fullFilePath = txbFullFilePath.Text.Trim();
             //判断文件是否存在
             if (!storageFile.FileExists(fullFilePath))
             {
                 txbReadContent.Text = "指定文件不存在";
                 return;
             }
             //读取
             using (StreamReader reader = new StreamReader(storageFile.OpenFile(fullFilePath, FileMode.Open)))
             {
                 txbReadContent.Text = reader.ReadToEnd();
             }
         }
        
     }
 }
原文地址:https://www.cnblogs.com/xyang/p/3678811.html