• [本地存储值]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
    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

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

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

    获取已经存储的值

    UserSettings.Instance.Get<int>("TestValue", 0);
  • 相关阅读:
    在微信小程序中使用富文本转化插件wxParse
    在微信小程序的JS脚本中使用Promise来优化函数处理
    结合bootstrap fileinput插件和Bootstrap-table表格插件,实现文件上传、预览、提交的导入Excel数据操作流程
    基于Metronic的Bootstrap开发框架经验总结(16)-- 使用插件bootstrap-table实现表格记录的查询、分页、排序等处理
    python 垃圾回收——分代回收 和java有些区别 注意循环引用无法被回收
    python del语句作用在变量上,而不是数据对象(常量)上
    for循环中的lambda与闭包——Python的闭包是 迟绑定 , 这意味着闭包中用到的变量的值,是在内部函数被调用时查询
    Python-try except else finally有return时执行顺序探究——finally语句无论如何也是会执行的
    你所不知道的Python | 字符串连接的秘密——连接大量字符串时 join和f-string都是性能最好的选择
    十大经典排序算法
  • 原文地址:https://www.cnblogs.com/liangyuwen/p/12892659.html
Copyright © 2020-2023  润新知