zoukankan      html  css  js  c++  java
  • ApplicationSettingsBase运用

    先建一个类继承于ApplicationSettingsBase

    using System;
    using System.ComponentModel;
    
    namespace Concert.Configuration
    {
    
        public sealed class UserSettings : System.Configuration.ApplicationSettingsBase, Concert.Configuration.IUserSettings
        {
    
            private static readonly bool ThrowOnErrorDeserializing = false, ThrowOnErrorSerializing = false;
            private static IUserSettings defaultInstance = ((UserSettings)System.Configuration.ApplicationSettingsBase.Synchronized(new UserSettings()));
            private static readonly System.Configuration.SettingsAttributeDictionary SettingsAttributes = new System.Configuration.SettingsAttributeDictionary() {
                {typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute()}
            };
    
            private System.Configuration.SettingsProvider provider;
    
            private UserSettings()
            {
            }
    
            public static IUserSettings Instance
            {
                get
                {
                    return defaultInstance;
                }
            }
    
            public void Register<T>(string name, T defaultValue)
            {
                if (name == null || name.Trim().Length == 0)
                    throw new ArgumentNullException("name");
                var property = this.Properties[name];
                if (property == null)
                    this.CreateSettingsProperty(name, typeof(T), defaultValue);
            }
    
            public bool Contains(string name)
            {
                if (name == null || name.Trim().Length == 0)
                    throw new ArgumentNullException("name");
                var property = this.Properties[name];
                return property != null;
            }
    
            public void Set<T>(string name, T value)
            {
                if (this.Contains(name) == false)
                    this.Register<T>(name, value);
                this[name] = value;
            }
    
            public T Get<T>(string name, T defaultValue)
            {
                if (name == null || name.Trim().Length == 0)
                    throw new ArgumentNullException("name");
                if (this.Contains(name))
                {
                    return (T)(this[name] ?? defaultValue);
                }
                else
                {
                    this.CreateSettingsProperty(name, typeof(T), defaultValue);
                    var val = this[name];
                    //if(val == null) this.Remove(name);                
                    return (T)(val ?? defaultValue);
                }
            }
    
            public void Remove(string name)
            {
                if (name == null || name.Trim().Length == 0)
                    throw new ArgumentNullException("name");
                //var property = this.Properties[key];
                //if (property != null)
                this.PropertyValues.Remove(name);
                this.Properties.Remove(name);
            }
    
            private void CreateSettingsProperty(string name, Type propertyType, object defaultValue)
            {
                var property = new System.Configuration.SettingsProperty(name, propertyType, this.Provider, false, defaultValue,
                    this.GetSerializeAs(propertyType), SettingsAttributes, ThrowOnErrorDeserializing, ThrowOnErrorSerializing);
                this.Properties.Add(property);
            }
    
            private System.Configuration.SettingsSerializeAs GetSerializeAs(Type type)
            {
                TypeConverter converter = TypeDescriptor.GetConverter(type);
                bool flag = converter.CanConvertTo(typeof(string));
                bool flag2 = converter.CanConvertFrom(typeof(string));
                if (flag && flag2)
                {
                    return System.Configuration.SettingsSerializeAs.String;
                }
                return System.Configuration.SettingsSerializeAs.Xml;
            }
    
            private System.Configuration.SettingsProvider Provider
            {
                get
                {
                    if (this.provider == null && (this.provider = this.Providers["LocalFileSettingsProvider"]) == null)
                    {
                        this.provider = new System.Configuration.LocalFileSettingsProvider();
                        this.provider.Initialize(null, null);
                        this.Providers.Add(this.provider);
                    }
                    return this.provider;
                }
            }
    
        }
    
    }
    
    UserSettings
    View Code

    再建一个接口类

    using System.ComponentModel;
    namespace Concert.Configuration
    {
        public interface IUserSettings : INotifyPropertyChanged
        {
            void Register<T>(string name, T defaultValue);
            bool Contains(string name);
            //object Get(string name, object defaultValue);
            T Get<T>(string name, T defaultValue);
            void Set<T>(string name, T value);
    
            void Reload();
            void Save();
            void Upgrade();
    
        }
    }
    
    IUserSettings
    View Code

    存储值到本地,值将会被保存到系统盘个人文件夹目录里

    UserSettings.Instance.Set<int>("TestValue", 23456);
    UserSettings.Instance.Save();

    获取已经存储的值

    UserSettings.Instance.Get<int>("TestValue", 0);

    转载:http://www.cnblogs.com/PanLiang/p/4723507.html





  • 相关阅读:
    C语言编程获取PE文件导出表内容
    C语言编程获取PE文件导入函数
    C语言编程获取PE文件Section_Header
    C语言编程获取PE文件Option_Header
    C语言编程获取PE文件File_Header内容
    C语言编程获取PE文件DOS头
    Spring源码剖析开篇:什么是Spring?
    重新学习Mysql数据13:Mysql主从复制,读写分离,分表分库策略与实践
    重新学习MySQL数据库12:从实践sql语句优化开始
    重新学习MySQL数据库10:MySQL里的那些日志们
  • 原文地址:https://www.cnblogs.com/tianciliangen/p/6025990.html
Copyright © 2011-2022 走看看