打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
利用IsolatedStorageFile(隔离存储文件)与HashTable结合持久化应...
IsolatedStorageFile是MS推出的用来安全存储应用程序信息的一种安全方式。是信息持久化的理想的存储方式。
HashTable可以存储任意可以序列化的实例。So,如果将两种方式结合,就可以持久化任何可以序列化的实例到硬盘上面,而且是很安全。
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
 [Serializable]
    public class ApplicationStorage : Hashtable {
        #region Private fields

        // File name. Let us use the entry assembly name with .dat as the extension.
        private string settingsFileName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name + ".dat";

        #endregion

        #region Constructor

        // The default constructor.
        public ApplicationStorage() {
            LoadData();
        }

        // This constructor is required for deserializing our class from persistent storage.
        protected ApplicationStorage(SerializationInfo info, StreamingContext context)
            : base(info, context) {
        }

        #endregion

        #region Private methods

        private void LoadData() {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
            if (isoStore.GetFileNames(settingsFileName).Length == 0) {
                // File not exists. Let us NOT try to DeSerialize it.
                return;
            }

            // Read the stream from Isolated Storage.
            Stream stream = new IsolatedStorageFileStream(settingsFileName, FileMode.OpenOrCreate, isoStore);
            if (stream != null) {
                try {
                    // DeSerialize the Hashtable from stream.
                    IFormatter formatter = new BinaryFormatter();
                    Hashtable appData = (Hashtable)formatter.Deserialize(stream);

                    // Enumerate through the collection and load our base Hashtable.
                    IDictionaryEnumerator enumerator = appData.GetEnumerator();
                    while (enumerator.MoveNext()) {
                        this[enumerator.Key] = enumerator.Value;
                    }
                }
                finally {
                    // We are done with it.
                    stream.Close();
                }
            }
        }

        #endregion

        #region Public Methods

        public void ReLoad() {
            LoadData();
        }

        /// <summary>
        /// Saves the configuration data to the persistent storage.
        /// </summary>
        public void Save() {
            // Open the stream from the IsolatedStorage.
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
            Stream stream = new IsolatedStorageFileStream(settingsFileName, FileMode.Create, isoStore);

            if (stream != null) {
                try {
                    // Serialize the Hashtable into the IsolatedStorage.
                    IFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(stream, (Hashtable)this);
                }
                finally {
                    stream.Close();
                }
            }
        }

        #endregion
    }
该类集成hashtable,因此拥有Hashtable 的功能,该类的两个重要方法,一个是Load 一个是Save。一个是将流序列化成对象,一个是将对象序列化为流。
用法如下:
            ApplicationStorage storage = new ApplicationStorage();
            if (storage["Key1"] != null) {
                object ht = storage[Key1];
                if (ht != null) {
                       ...
                    }
                }
其实就像hashtable 一样用啦!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
在线CHM阅读器(2)——文件提取及关键文件解析
遍历Hashtable 的几种方法
SilverLight浏览器交互之:SilverLight用户独立存储空间中文件的创建和读取
Hashtable和HashMap的区别
C# 方法使用汇总(GetEnumerator用法,??用法等)
几个C#编程的小技巧
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服