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) 文件打开 ...
随机推荐
- 使用ajax实现搜索功能
最近要做一个搜索功能,网上搜了一圈,终于做出来了,很简单的一个,这里分享我的方法,希望对大家有用,不足之处还请指教. 这里使用ajax提交数据,配合jquery将数据显示出来. 用jq的keyup ...
- HDU 3068 最长回文( Manacher模板题 )
链接:传送门 思路:Manacher模板题,寻找串中的最长回文子串 /***************************************************************** ...
- BlankUtil(判断是否为空和去除多余空格)
package com.drn.core.util; import java.io.Serializable; import java.util.Map; import java.util.Prope ...
- linux查看前几条命令记录
1.按上下箭头键2.history|more分页显示3.vi /etc/profile找HISTSIZE=1000,说明你最多能存1000条历史记录.4.!!执行最近执行的命令5.history|he ...
- [luogu] P3294 [SCOI2016]背单词 (贪心)
题目描述 Lweb 面对如山的英语单词,陷入了深深的沉思,"我怎么样才能快点学完,然后去玩三国杀呢?".这时候睿智的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他 ...
- 常见的版本号及Springcloud的版本
谈谈软件版本号的认识 一.常见版本号说明 举个瓜:2.0.3 RELEASE 2:主版本号,当功能模块有较大更新或者整体架构发生变化时,主版本号会更新 0:次版本号.次版本表示只是局部的一些变动. 2 ...
- https的基本原理,看完你的程序员女朋友再也不和你提分手了
[http风险] 首先,我们来讲一下平时上网的时候,存在的风险. 初步接触过网络的同学都知道,网络上是很不安全的,尤其是各种公共场合的免费WIFI,以及手机上各种免费上网的万能钥匙.这些不安全因素会导 ...
- HDOJ 1753 大明A+B
JAVA大数.... 大明A+B Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【LDA】动手实现LDA
这段时间对LDA比較感兴趣,尝试在工作中使用它.平时做想法的高速验证,都用的是"GibbsLDA++-0.2",一个c实现版本号的LDA. 这两天用c++ stl自己写了一个单机版 ...
- Android 6.0 开发人员对系统权限的使用与练习(Permissions Best Practices)
Permissions Best Practices 在安装的过程中,用户非常easy忽略权限请求. 假设一个用户相应用感觉沮丧或者操心泄漏个人信息,那么这些用户就会不用他或者卸载它. 怎样规避这个问 ...