先建一个类继承于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. Input常用的输入框验证(正则)

    1.只是不能输入空格 <input type="text" οnkeyup="this.value=this.value.replace(/^ +| +$/g,'' ...

  2. JS 循环遍历 总结

    一.循环遍历语句 for...in... (ES5) 语法:javascript for(keys in obj){} 适用:遍历对象 说明: 1.keys表示obj对象的每一个键值对的键(键名),所 ...

  3. HDU-3605-Escape(最大流, 状态压缩)

    链接: https://vjudge.net/problem/HDU-3605 题意: 2012 If this is the end of the world how to do? I do not ...

  4. [每日一讲] Python系列:列表与元组

    参考文档 https://docs.python.org/zh-cn/3.7/tutorial/introduction.html#lists """ DATA STRU ...

  5. HTML5基础——笔记

    HTML5基础——笔记 近几年来,互联网+.大数据.云计算‘物联网‘虚拟现实‘人工智能.机器学习.移动互联网等IT相关新名词.新概念层出不穷,相关产业发展如火如荼.互联网+移动互联网已经深入到人民日常 ...

  6. 【leetcode】LCP 3. Programmable Robot

    题目如下: 力扣团队买了一个可编程机器人,机器人初始位置在原点(0, 0).小伙伴事先给机器人输入一串指令command,机器人就会无限循环这条指令的步骤进行移动.指令有两种: U: 向y轴正方向移动 ...

  7. shell练习--PAT题目1004: 成绩排名 !(失败案例,shell运行超时)

    读入 n(>)名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式: 每个测试输入包含 1 个测试用例,格式为 第 1 行:正整数 n 第 2 行:第 1 个学生的姓 ...

  8. linux-PXE-12

    以DHCP+DNS模式管理服务器IP地址和主机名.服务器上架前,以其MAC地址为依据,在DHCP中配置主机保留并分配主机名.DHCP结合TFTP提供的PXE服务,提供PXE引导Linux内核和启动镜像 ...

  9. PHP基础教程 PHP的页面缓冲处理机制

    PHP有很多机制.函数,其实就是魔术师,重复发挥好,其实甚至是简单应用,就会出现神奇的效果.兄弟连PHP培训 这里来讲一个ob_start()函数. ob_start()函数用于打开缓冲区,比如hea ...

  10. Codeforces 960F Pathwalks ( LIS && 树状数组 )

    题意 : 给出若干个边,每条边按照给出的顺序编号,问你找到一条最长的边权以及边的编号同时严格升序的一条路径,要使得这条路径包含的边尽可能多,最后输出边的条数 分析 :  这题和 LIS 很相似,不同的 ...