在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 存储信息

最新文章

  1. Jmeter之参数化
  2. html与Android——webView
  3. 查看死锁 的存储过程 ,工具存储过程 sp_who_lock
  4. Android之使用Volley框架在ListView中加载大量图片
  5. Android 中的code sign
  6. DS实验题 击鼓传花
  7. saltstack之(十)扩展组件salt-ssh
  8. Java工具类 Apache Commons:commons-lang
  9. C++中为什么要用虚函数、指针或引用才能实现多态?
  10. scheme Continuation
  11. 柯南君:看大数据时代下的IT架构(5)消息队列之RabbitMQ--案例(Work Queues起航)
  12. HDU 2509 Be the Winner(取火柴博弈2)
  13. Rsync + sersync 实时同步备份
  14. Flask框架(1)--基础
  15. free命令查看内存信息
  16. 【阿里聚安全·安全周刊】Intel芯片级安全漏洞事件|macOS存在漏洞
  17. c语言 贪食蛇小游戏
  18. Kafka实战-简单示例
  19. WDA基础六:字段,表等visiable,enable,read_only控制
  20. 【Kafka】Kafka-数据倾斜问题-参考资料-解决方案

热门文章

  1. 用于构建 RESTful Web 服务的多层架构
  2. HDU 1028 Ignatius and the Princess III(母函数整数拆分)
  3. UVALive-8077 Brick Walls 找规律
  4. CF482C Game with Strings (状压DP+期望DP)
  5. ubuntu/wireshark --Lua: Error during loading: [string "/usr/share/wireshark/init.lua"]:45问题解决
  6. react生命周期知识点
  7. 微信小程序开发入门(一)
  8. LoadRunner监控Window/Unix系统资源的配置
  9. ASP.NET-POSTBACK是什么
  10. 极路由4pro交叉编译c、c++的代码