在C#中还有一种叫做IsolatedStorage的存储机制,他存储信息的方式类似于我们的cookie, IsolatedStorage存储独立于每一个application,换句话说我们加载多个应用程序是他们不会相互影响,我们这样就可以在 下次运行的时从IsolatedStorage中提取一些有用的数据,这对我们来说是很好的一件事吧~

使用islatedstorage也十分简单,不废话了 还是上个实例看吧.

先获取一个IsolatedStorage文件对象.

随后我们将创获取IsolatedStorageFileStream对象,再以文件流的形式写入和读取

注:(using System.IO.IsolatedStorage;using System.IO;)

 static void Main(string[] args)
{
string fileName = "test.txt";
SaveData("测试内容.", fileName);
string content = LoadData(fileName);
Console.ReadKey();
}
//保存
static void SaveData(string _data, string _fileName)
{
//如果尝试使用此方法 ClickOnce 或基于 Silverlight 的应用程序之外,你将收到IsolatedStorageException异常,因为不能确定调用方的应用程序标识。
//using (IsolatedStorageFile myIsf = IsolatedStorageFile.GetUserStoreForApplication()) using (IsolatedStorageFile isf = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
{
//var availableFreeSpace = isf.AvailableFreeSpace.ToString() + " bytes";//这里是剩余空间
//var quota = isf.Quota.ToString() + " bytes";//这里是当前的限额
//var usedSpace = (isf.Quota - isf.AvailableFreeSpace).ToString() + " bytes";//这里是用户已经使用的空间 //if (true == isf.IncreaseQuotaTo(1048576 * 2))//将限额增加到2MB(注: 这里单位是bytes) 新配额必须大于旧配额
//{
// quota = isf.Quota.ToString() + " bytes";// 限额
//}
//else
//{
// var Text = "限额更改失败.";
//} using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(_fileName, FileMode.OpenOrCreate, isf))
{
//获取文件路径
//string filePath = isolatedStorageFileStream.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(isolatedStorageFileStream).ToString();
using (StreamWriter mySw = new StreamWriter(isolatedStorageFileStream))
{
mySw.BaseStream.Seek(, SeekOrigin.End); //追加(写入位置)
mySw.Write(_data);
mySw.Close();
}
}
}
}
//读取
static string LoadData(string _fileName)
{
string data = String.Empty;
using (IsolatedStorageFile myIsf = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
{
using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(_fileName, FileMode.Open, myIsf))
{ //获取文件路径
// string filePath = isolatedStorageFileStream.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(isolatedStorageFileStream).ToString();
using (StreamReader sr = new StreamReader(isolatedStorageFileStream))
{
string lineOfData = string.Empty;
while ((lineOfData = sr.ReadLine()) != null)
data += lineOfData;
}
}
}
return data;
} }

参考:

IsolatedStorageFile.GetUserStoreForApplication

使用 IsolatedStorageFileStream 存储信息

System.IO.IsolatedStorage 使用 IsolatedStorageFileStream 存储信息的更多相关文章

  1. System.IO.Pipelines: .NET上高性能IO

    System.IO.Pipelines是一个新的库,旨在简化在.NET中执行高性能IO的过程.它是一个依赖.NET Standard的库,适用于所有.NET实现. Pipelines诞生于.NET C ...

  2. System.IO.Pipelines: .NET高性能IO

    System.IO.Pipelines是一个新的库,旨在简化在.NET中执行高性能IO的过程.它是一个依赖.NET Standard的库,适用于所有.NET实现. Pipelines诞生于.NET C ...

  3. C# System.IO.StreamWriter

    实现一个 TextWriter,使其以一种特定的编码向流中写入字符. using System; using System.Collections.Generic; using System.Linq ...

  4. C# System.IO和对文件的读写操作

      System.IO命名空间中常用的非抽象类 BinaryReader 从二进制流中读取原始数据 BinaryWriter 从二进制格式中写入原始数据 BufferedStream 字节流的临时存储 ...

  5. ZedGraph的曲线的LineItem对象的Tag属性存储信息进而在鼠标悬浮时进行显示

    场景 Winform中设置ZedGraph鼠标悬浮显示距离最近曲线上的点的坐标值和X轴与Y轴的标题: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article ...

  6. 【等待事件】等待事件系列(3+4)--System IO(控制文件)+日志类等待

     [等待事件]等待事件系列(3+4)--System IO(控制文件)+日志类等待   1  BLOG文档结构图     2  前言部分   2.1  导读和注意事项 各位技术爱好者,看完本文后,你可 ...

  7. .net System.IO.Stream 流操作类(FileStream等)

    Stream 是所有流的抽象基类.流是字节序列的抽象概念. 流涉及到的3个基本操作: 读取流,读取是指从流到数据结构(如字节数组)的数据传输. 写入流,写入是指从数据结构到流的数据传输. 流查找,查找 ...

  8. Type 'System.IO.FileStream' with data contract name 'FileStream:http://schemas.datacontract.org/2004/07/System.IO' is not expected.

    今天在WCF项目里使用DataContract序列化接口参数的时候,报了这个错,错误详细信息如下: System.ServiceModel.CommunicationException: There ...

  9. 详解C#中System.IO.File类和System.IO.FileInfo类的用法

    System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间.下面通过程序实例来介绍其主要属性和方法. (1) 文件打开 ...

随机推荐

  1. css——定位

    position absolute:绝对定位 1.以页面的左上角为原点 2.不保留原来的位置 3.z-index可以调整图层顺序 如果想实现以父级元素左上角为原点.则:父级相对,子级绝对 在子级绝对的 ...

  2. 使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务

    作者: Yi Ming Huang, 软件工程师, IBM Dong Fei Wu, 软件工程师, IBM Qing Guo, 软件工程师, IBM 出处: http://www.ibm.com/de ...

  3. BZOJ 4864 [BJWC2017]神秘物质 (splay)

    题目大意: 让你维护一个序列,支持: 1.合并两个相邻的数为一个新的数 2.在某个位置插入一个数 3.查询一个区间的任意子区间极差的最大值 4.查询一个区间的任意子区间极差的最小值 前两个操作可以用$ ...

  4. C语言 将十六进制字符串转为十六进制数 (二进制、十进制都适用)

    主要利用 long int strtol(const char *nptr,char **endptr,int base); 函数 函数说明: 参数base范围从2至36,或0.参数base代表采用的 ...

  5. JavaString库

    String库 .length() 字符串的长度,一个字符串为空(空字符串对象)和null(不指向任何对象)是两个概念,中文字符和英文字符是一样的计数(一个中文是一个字符,一个英文字母是一个字符) . ...

  6. jquery-常用插件集合

    001.弹出消息插件toastr https://github.com/CodeSeven/toastr 002.弹出页面全屏插件 https://github.com/sindresorhus/sc ...

  7. [AngularJS]Chapter 3 使用AngularJS构建应用程序

    本章内容提要: 如何布置AngularJS进行快速开发 开启服务器进行测试 使用Karma进行单元测试用例测试 编译压缩AngularJS进行生产 使用Batarang进行Debug 如何简化开发工作 ...

  8. SharePoint 2010 安装教程

    SharePoint Server 2010作为MOSS 2007的升级版本,自从2009年底发布Beta版本以来就备受关注,网络上已经出现了很多相关的文章,其中也不乏中文的信息. 最近SharePo ...

  9. c++友元实现操作符重载

    运算符重载的本质是一个函数 #include <iostream> using namespace std; class A { private: int m_a; int m_b; fr ...

  10. OS - 线程和进程的差别

    进程是资源分配的基本单位,又是调度执行的基本单位.比如.用户执行自己的程序,系统就创建一个进程.并为它分配资源,包含各种表.内存空间.磁盘空间.I/O设备等. 然后.把该进程放入进程的就绪队列.进程调 ...