【WP之一】]独立存储

介绍:html

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

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

 

独立设置存储:
命名空间为: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();

独立文件存储:spa

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

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

经常使用操做:对象

...
 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.htmlblog