C# 应用程序设置
官方参考:http://msdn.microsoft.com/zh-cn/library/k4s6c3a0(v=VS.80).aspx
使用VS自带的应用程序设置功能
- 创建项目
- 选择菜单 [项目] > [属性]
- 选择 [设置]
就可手动添加应用程序设置了。
添加成功后,系统会自动生成App.config文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?
xml
version="1.0" encoding="utf-8"?>
<
configuration
>
<
userSettings
>
<
WindowsApplication5.Properties.Settings
>
<
setting
name="mySet" serializeAs="String">
<
value
>testSet_828</
value
>
</
setting
>
<
setting
name="FormTitle" serializeAs="String">
<
value
>FormTestdddd</
value
>
</
setting
>
</
WindowsApplication5.Properties.Settings
>
</
userSettings
>
</
configuration
>
|
关于User和Application的区别
- Application 不允许在程序中更新设置。只能手动更改App.config或到项目属性的设置中更改。
- User 允许在程序中更改设置。
VS也提供了一种直接在窗体控件属性的ApplicationSettings 里设置关联应用程序的快捷方法。
以下列举了使用VS自带应用程序应注意的地方
- 如果范围是Application时,在程序此值时只读的。只能通过修改App.config的对应项来更改。
- 如果范围是User,并且在程序未对此值做修改时,修改App.config对应项,在程序访问时当前值为App.config中设置的值。
- 如果范围是User,并且在程序中对此值进行了修改,App.config中记录的还会是老值,并且以后的App.config此项设置将无效。
那到底User修改后的值系统在什么地方存这呢?
经过测试是在C:\Documents and Settings\Administrator\Local Settings\Application Data\Phook\WindowsApplication5.exe_Url_nlwmvagksxwiigfpn5ymssyrjtyn22ph\1.0.0.0\user.config
下存着,如果更改以上App.config则程序将取到新值。很奇怪,微软为什么要弄的怎么复杂。
在程序中使用
1
2
3
4
5
6
|
//获取值
label1.Text = Properties.Settings.Default.mySet;
label2.Text = Properties.Settings.Default.myApp;
//修改值,只能修改User范围的。
Properties.Settings.Default.mySet =
"test1111"
;
Properties.Settings.Default.Save();
|
总结
- 应该是如果选择范围是User时,此设置是用户级别的。使用不同用户登录后运行程序取的值是不一样的。
- 并且如果程序修改了名称,各自会拥有另一套App.config。
- 而Application是应用程序级的,任何打开相同程序的Application都会一样。
自定义App.config
可能你想要一个Config可以功能和Application范围一样,但有同时支持程序修改。以下是实现方法
创建工程
手动添加App.config
格式如下:
1
2
3
4
5
6
|
<?
xml
version="1.0" encoding="utf-8" ?>
<
configuration
>
<
appSettings
>
<
add
key="y" value="this is Y"/>
</
appSettings
>
</
configuration
>
|
引用 System.Configuration
把对App.config的操作合并成了一个类方便调用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Configuration;
//注意需要引用 System.Configuration
public
class
AppConfig
{
private
static
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
/// <summary>
/// 获取
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public
static
string
GetValue(
string
key)
{
string
strReturn =
null
;
if
(config.AppSettings.Settings[key] !=
null
)
{
strReturn = config.AppSettings.Settings[key].Value;
}
return
strReturn;
}
/// <summary>
/// 设置
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public
static
void
SetValue(
string
key,
string
value)
{
if
(config.AppSettings.Settings[key] !=
null
)
{
config.AppSettings.Settings[key].Value = value;
}
else
{
config.AppSettings.Settings.Add(key, value);
}
config.Save(ConfigurationSaveMode.Modified);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="key"></param>
public
static
void
DelValue(
string
key)
{
config.AppSettings.Settings.Remove(key);
}
}
|
使用方法
1
2
3
4
|
//修改或添加
AppConfig.SetValue(
"dtNow"
, DateTime.Now.Millisecond.ToString());
//获取
label1.Text = AppConfig.GetValue(
"dtNow"
);
|