System.IO.IsolatedStorage 使用 IsolatedStorageFileStream 存储信息
在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 存储信息的更多相关文章
- System.IO.Pipelines: .NET上高性能IO
System.IO.Pipelines是一个新的库,旨在简化在.NET中执行高性能IO的过程.它是一个依赖.NET Standard的库,适用于所有.NET实现. Pipelines诞生于.NET C ...
- System.IO.Pipelines: .NET高性能IO
System.IO.Pipelines是一个新的库,旨在简化在.NET中执行高性能IO的过程.它是一个依赖.NET Standard的库,适用于所有.NET实现. Pipelines诞生于.NET C ...
- C# System.IO.StreamWriter
实现一个 TextWriter,使其以一种特定的编码向流中写入字符. using System; using System.Collections.Generic; using System.Linq ...
- C# System.IO和对文件的读写操作
System.IO命名空间中常用的非抽象类 BinaryReader 从二进制流中读取原始数据 BinaryWriter 从二进制格式中写入原始数据 BufferedStream 字节流的临时存储 ...
- ZedGraph的曲线的LineItem对象的Tag属性存储信息进而在鼠标悬浮时进行显示
场景 Winform中设置ZedGraph鼠标悬浮显示距离最近曲线上的点的坐标值和X轴与Y轴的标题: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article ...
- 【等待事件】等待事件系列(3+4)--System IO(控制文件)+日志类等待
[等待事件]等待事件系列(3+4)--System IO(控制文件)+日志类等待 1 BLOG文档结构图 2 前言部分 2.1 导读和注意事项 各位技术爱好者,看完本文后,你可 ...
- .net System.IO.Stream 流操作类(FileStream等)
Stream 是所有流的抽象基类.流是字节序列的抽象概念. 流涉及到的3个基本操作: 读取流,读取是指从流到数据结构(如字节数组)的数据传输. 写入流,写入是指从数据结构到流的数据传输. 流查找,查找 ...
- 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 ...
- 详解C#中System.IO.File类和System.IO.FileInfo类的用法
System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间.下面通过程序实例来介绍其主要属性和方法. (1) 文件打开 ...
随机推荐
- android handler传递数据
起因:在android使用get请求获取验证码时需要重开一个线程,这就造成了我无法获取到从服务器后台返回的数据 解决方法:创建全局变量,将返回的数据解析后返回给handler,再在handler中将数 ...
- [LUOGU]P3701 主席树(假的)
有人恶意刷难度...就一个最大流模板... 但是题面吼啊2333 #include <iostream> #include <cstdio> #include <queu ...
- [luogu] P4823 [TJOI2013]拯救小矮人(贪心)
P4823 [TJOI2013]拯救小矮人 题目描述 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以 ...
- GitHub上传项目,使用desktop(客户端)教程
GitHub上传项目,使用desktop(客户端)教程 搜索“GitHub上传项目”,能得到很多相关的文章教程,里面讲的都特别麻烦,要弄什么ssh之类的,可算是吓坏了我,使我非常的怀疑为什么GitH ...
- Nutch命令大全
Nutch采用了一种命令的方式进行工作,其命令可以是对局域网方式的单一命令也可以是对整个Web进行爬取的分步命令.主要的命令如下: 1. Crawl Crawl是"org.apache.nu ...
- BZOJ——1602: [Usaco2008 Oct]牧场行走 || 洛谷—— P2912 [USACO08OCT]牧场散步Pasture Walking
http://www.lydsy.com/JudgeOnline/problem.php?id=1602 || https://www.luogu.org/problem/show?pid=2912 ...
- UI自动化
1.下载安装JDK,配置环境变量 2.下载eclips,并安装 3.下载jskuli截图工具,并安装 4.下载Jspec插件,将其放在eclipse路径下的\plugins文件夹中.(打开eclips ...
- 模拟退火算法c++
转载. 为方便理解, 在原博客的基础上加部分注释, 原博客地址:http://www.cnblogs.com/CsOH/p/6049117.html 今天终于用模拟退火过了一道题:CodeVS: P1 ...
- [NOI.AC#32]sort 构造
链接 50分做法(只有0,1) 根据归并排序的思想,假设我们现在已经把 \(l\dots mid\) 和 \(mid+1\dots r\) 排好序 只要把左边连续的1和右边连续的0翻转即可 inlin ...
- SaltStack介绍——SaltStack是一种新的基础设施管理方法开发软件,简单易部署,可伸缩的足以管理成千上万的服务器,和足够快的速度控制,与他们交流
SaltStack介绍和架构解析 简介 SaltStack是一种新的基础设施管理方法开发软件,简单易部署,可伸缩的足以管理成千上万的服务器,和足够快的速度控制,与他们交流,以毫秒为单位.SaltSta ...