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(0, 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 存储信息

 

posted on 2019-03-23 10:17 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10582724.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值