ZFYCH_Love

Simply but Powerful

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  115 随笔 :: 1 文章 :: 36 评论 :: 18万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

介绍:

提供一个磁盘存储空间,他是一种虚拟的文件系统,能存储小量的数据;在默认的情况下,它只能存储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();
             }
         }
        
     }
 }
复制代码

 

posted on   xiaoyang_  阅读(693)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示