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 == )
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运用的更多相关文章
- VS C#开发中WinForm中Setting.settings的作用
.定义 在Settings.settings文件中定义配置字段.把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改.可以使用数据网格视图,很方便: .读取配置值 tex ...
- A WPF/MVVM Countdown Timer
Introduction This article describes the construction of a countdown timer application written in C# ...
- 【.net 深呼吸】自己动手来写应用程序设置类
在开始装逼之前,老周先说明一件事.有人说老周写的东西太简单了,能不能写点复杂点.这问题就来了,要写什么东西才叫“复杂”?最重要的是,写得太复杂了,一方面很多朋友看不懂,另一方面,连老周自己也不知道怎么 ...
- 在windows2012上安装MSSQL 2008 Manage Studio 出现错误
在windows2012上安装MSSQL 2008 Manage Studio 出现错误: 解决方法:重新建立一个管理员账户,用另外一个账户登陆,然后安装. 原因:未知 --------------- ...
- C# winform中Setting.settings 相关知识点
1.在Settings.settings文件中定义配置字段.包含字段名.类型.范围.值四部分的属性. 字段名.类型和值类似编程中字段的定义一样使用,不再过多的解释.重点讲一下”范围“字段的含义与区别. ...
- VS2008中的配置文件app.config简单小结
应用程序的配置文件用于读取和保存简单的本地数据,vs中新增配置文件可以直接在项目的”属性“-”设置“里添加,添加后在项目的Properties文件夹会多出一组两个文件:Settings.setting ...
- C# Settings.settings的用处
1.定义 在Settings.settings文件中定义配置字段.把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改.可以使用数据网格视图,很方便: 2.读取配置值 t ...
- C# Setting.settings . 用法
1.定义 在Settings.settings文件中定义配置字段.把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改.可以使用数据网格视图,很方便: 2.读取配置值 t ...
- C#中使用设置(Settings.settings) Properties.Settings.Default .(配置文件相当重要)
C#中使用设置(Settings.settings) Properties.Settings.Default . 2016年08月04日 15:02:43 zxd9790902 阅读数:10664更多 ...
随机推荐
- 机器学习-聚类(clustering)算法:K-means算法
1. 归类: 聚类(clustering):属于非监督学习(unsupervised learning) 无类别标记(class label) 2. 举例: 3. Kmeans算法 3.1 clust ...
- Redis集群部署一直卡在Waiting for the cluster to join ......(Redis集群总线配置)
redis集群总线端口为redis客户端端口加上10000,比如说你的redis 6379端口为客户端通讯端口,那么16379端口为集群总线端口 我搭建的redis集群中端口号是从 7001 ~ 70 ...
- Java 判断是否为回文字符串
回文字符串有两种:abcba,abccba. 代码: static boolean func(String str) { int len = str.length(); for (int i = 0; ...
- MSDN上对yield关键字的示例
public class PowersOf2 { static void Main() { // Display powers of 2 up to the exponent of 8: , )) { ...
- python中的正则表达式的使用
一.正则表达式简介 正则表达式的官方文档:https://www.runoob.com/regexp/regexp-tutorial.html 正则表达式:又称正规表示式.正规表示法.正规表达式.规则 ...
- 【writeup】is_numeric函数矛盾运用
最近在BugkuCTF平台刷题,遇到‘矛盾’http://120.24.86.145:8002/get/index1.php,感觉蛮有意思的,记录下思路 目标代码如下: $num=$_GET['num ...
- linux批量设置部分文件与文件夹权限
批量设置web目录下文件.文件夹名为sitemap.xml.sitemap 属性为777 cd /www/web find ./ -name 'sitemap' -exec chmod -R 77 ...
- node模块管理
淘宝镜像:npm install -g cnpm --registry=https://registry.npm.taobao.org
- 解读sam格式文件
1,SAM文件格式介绍 SAM(The Sequence Alignment / Map format)格式,即序列比对文件的格式,详细介绍文档:http://samtools.github.io/h ...
- PyQt4解析HTML Dom
环境: Windows 10 Python 2.7.10 0x01 安装PyQt4 在这个页面下载,注意选对版本. https://riverbankcomputing.com/software/py ...