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

再建一个接口类

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);

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

				
时间: 2024-11-04 16:09:07

ApplicationSettingsBase运用的相关文章

VS C#开发中WinForm中Setting.settings的作用

1.定义 在Settings.settings文件中定义配置字段.把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改.可以使用数据网格视图,很方便: 2.读取配置值 text1.text = Properties.Settings.Default.FieldName; //FieldName是你定义的字段 3.修改和保存配置 Properties.Settings.Default.FieldName = "server"; Properties.Sett

【.net 深呼吸】自己动手来写应用程序设置类

在开始装逼之前,老周先说明一件事.有人说老周写的东西太简单了,能不能写点复杂点.这问题就来了,要写什么东西才叫“复杂”?最重要的是,写得太复杂了,一方面很多朋友看不懂,另一方面,连老周自己也不知道怎么表述. 而且,老周也不能把以前在K公司.Z公司和T公司中做项目的东西写出来的,其实嘛,工作中的编程没什么可写的,无非就是 select.insert.delete.update,无非就是连接数据库,断开连接,同步一下数据,把数据变成XML或JSON再发给另一终端.无非就是读读你的网卡CPU硬盘序列号

A WPF/MVVM Countdown Timer

Introduction This article describes the construction of a countdown timer application written in C# and WPF, using Laurent Bugnion's MVVMLight Toolkit. This article is based on the work of some previous articles I've written: A WPF Short TimeSpan Cus

C#各种配置文件使用,操作方法总结

配置文件操作 配置文件一般分为内置配置文和用户自定义配置文件. 内置配置文件包括app.config.web.config.Settings.settings等等. 用户自定义配置文件一般是将配置信息放到XML文件或注册表中,配置信息一般包括程序设置,记录运行信息,保存控件的信息(比如位置,样式). 一.内置配置文件操作 app.config和web.config操作类似,以app.config为例,Settings.settings能够指定值的类型和范围. 1.app.config文件操作 该

在windows2012上安装MSSQL 2008 Manage Studio 出现错误

在windows2012上安装MSSQL 2008 Manage Studio 出现错误: 解决方法:重新建立一个管理员账户,用另外一个账户登陆,然后安装. 原因:未知 ------------------------------ 关调用实时(JIT)调试而不是此对话框的详细信息, 请参见此消息的结尾. ************** 异常文本 ************** System.Configuration.ConfigurationErrorsException: 创建 userSett

VS2008中的配置文件app.config简单小结

应用程序的配置文件用于读取和保存简单的本地数据,vs中新增配置文件可以直接在项目的”属性“-”设置“里添加,添加后在项目的Properties文件夹会多出一组两个文件:Settings.settings和Settings.Designer.cs,前者是一个xml文件用于vs界面的显示,后者是一个vs生成的继承了System.Configuration.ApplicationSettingsBase的类Setting,该类是强类型化的,因此可以方便地通过代码读取和保存配置项: 另外,也可以右键”项