前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【WP之一】]独立存储

【WP之一】]独立存储

作者头像
小端
发布2018-04-16 11:03:22
6620
发布2018-04-16 11:03:22
举报
文章被收录于专栏:java架构师

介绍:

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

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

代码语言:javascript
复制
独立设置存储:
命名空间为:System.IO.IsolatedStorage;主要涉及System.IO.IsolatedStorage.IsolatedStorageSettings类。
常用操作:
代码语言:javascript
复制
//创建操作独立设置存储必须的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()                  移除所有的文件和文件夹

常用操作:

代码语言:javascript
复制
...
 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();
             }
         }
        
     }
 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-04-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档