先建一个类继承于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 == )
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 == )
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 == )
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 == )
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", );
UserSettings.Instance.Save();

获取已经存储的值

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

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

												

ApplicationSettingsBase运用的更多相关文章

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

    .定义 在Settings.settings文件中定义配置字段.把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改.可以使用数据网格视图,很方便: .读取配置值 tex ...

  2. A WPF/MVVM Countdown Timer

    Introduction This article describes the construction of a countdown timer application written in C# ...

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

    在开始装逼之前,老周先说明一件事.有人说老周写的东西太简单了,能不能写点复杂点.这问题就来了,要写什么东西才叫“复杂”?最重要的是,写得太复杂了,一方面很多朋友看不懂,另一方面,连老周自己也不知道怎么 ...

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

    在windows2012上安装MSSQL 2008 Manage Studio 出现错误: 解决方法:重新建立一个管理员账户,用另外一个账户登陆,然后安装. 原因:未知 --------------- ...

  5. C# winform中Setting.settings 相关知识点

    1.在Settings.settings文件中定义配置字段.包含字段名.类型.范围.值四部分的属性. 字段名.类型和值类似编程中字段的定义一样使用,不再过多的解释.重点讲一下”范围“字段的含义与区别. ...

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

    应用程序的配置文件用于读取和保存简单的本地数据,vs中新增配置文件可以直接在项目的”属性“-”设置“里添加,添加后在项目的Properties文件夹会多出一组两个文件:Settings.setting ...

  7. C# Settings.settings的用处

    1.定义 在Settings.settings文件中定义配置字段.把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改.可以使用数据网格视图,很方便: 2.读取配置值 t ...

  8. C# Setting.settings . 用法

    1.定义 在Settings.settings文件中定义配置字段.把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改.可以使用数据网格视图,很方便: 2.读取配置值 t ...

  9. C#中使用设置(Settings.settings) Properties.Settings.Default .(配置文件相当重要)

    C#中使用设置(Settings.settings) Properties.Settings.Default . 2016年08月04日 15:02:43 zxd9790902 阅读数:10664更多 ...

随机推荐

  1. Vue配置bs环境

    安装插件 jQuery >: cnpm install jquery vue/cli 3 配置jQuery:在vue.config.js中配置(没有,手动项目根目录下新建) const webp ...

  2. String,StringBuilder和StringBuffer

    String 字符串常量,由String创建的字符内容,长度是不可改变,存放字符的数组被声明为final. 实际上String类操作字符串是通过建立一个StringBuffer,然后调用append( ...

  3. 通过lua扩展nginx

    1. 安装 准备主要的三个安装包,分别是 nginx-1.15.9.tar.gz LuaJIT-2.0.5.tar.gz lua-nginx-module-0.10.14.tar.gz 相关版本可以去 ...

  4. [易学易懂系列|rustlang语言|零基础|快速入门|(8)|Operators操作符]

    [易学易懂系列|rustlang语言|零基础|快速入门|(8)] 有意思的基础知识 Operators 我们今天再来看看操作符. 算术运算 操作符: + - * / % 代码如下 : let a = ...

  5. Tableau Dashboard

    Dashboard仪表盘,用来展示多个图表,并展示之间的联动,分析数据.

  6. Vue分割音乐歌词数据函数

    parseLyric(lyric) {       var lines = lyric.split(/\n/); //使用/n换行,将数据切成一个数组       var getLtricTime = ...

  7. java课堂动手测试

    课堂实验3 一个Java类文件中真的只能有一个公有类吗? 经过测试,当含有两个public  类时会报错,不能执行,假如删除第二个public则可以正常生成,说明一个java文件只能有一个公有类. 课 ...

  8. Flask【第11篇】:整合Flask中的目录结构

    整合Flask中的目录结构 一.SQLAlchemy-Utils 由于sqlalchemy中没有提供choice方法,所以借助SQLAlchemy-Utils组件提供的choice方法 import ...

  9. Shell-07数组与字符串

    Shell-07数组与字符串 数组 数组说白了就是一段连续的变量,一段连续的内存存储空间 解决:变量过多的问题:在同类的变量中,我们不需要去定义多个名字,而是以数组的方式来定义:(列表) 数组名 索引 ...

  10. Linux基础教程 linux awk内置变量使用介绍

    awk是个优秀文本处理工具,可以说是一门程序设计语言.下面是兄弟连Linux培训 给大家介绍的awk内置变量. 一.内置变量表 属性 说明 $0 当前记录(作为单个变量) $1~$n 当前记录的第n个 ...