在做 web 项目的时候,我一直在用 web.config ,觉得这个东西实在不错。偶然间发现,原来 winform 项目也有一个类似于 web.config 的文件——app.config,和呵,不胜欣喜!
一、添加app.config
方法一:手工添加
要添加这个文件很简单,执行如下操作就可以了:在开发环境中选择“项目”——>“添加新项”——>“XML 文件”,输入xml 文件的名称为 app.config。
然后就可以如同 web.config 一样,编辑 app.config 文件,以及在应用程序中读、写该配置文件。
重新编译项目,在应用程序的运行目录下会生成一个 app.config 的副本,名称为:程序名.exe.config。
方法二:系统自动添加
假设我们现在要使用 app.config 来动态配置一个 label 的 text 属性,那么,在label的属性窗口中选择如下图的属性进行配置,那么就可以让系统自动添加一个 app.config 的文件,呵呵,很省事的了。
二、编辑和使用app.config
1、增加自定义的键值
这个当然是增加<appSettings>标签了。下面给个例子:







根据应用的需要,我们可以添加许多需要动态配置的信息,如数据库连接字符串等。
2、编辑系统原有的属性
在下面的代码中,我配置了系统调试信息,这个东西挺不错的,可以将系统运行时输出的调试信息放置到一个文件。在我们使用 try ... catch 的时候,将 catch 到的信息输出给用户并不理想,因为用户看到一大堆的错误信息会感到很头痛,他们只希望知道操作成功还是没有成功。所以,将运行时的错误信息放置到文件是不错的选择。代码如下:














1
using
System;
2
using
System.Drawing;
3
using
System.Collections;
4
using
System.ComponentModel;
5
using
System.Windows.Forms;
6
using
System.Data;
7
8
using
System.Diagnostics ;
9
using
System.Configuration ;
10
11
namespace
AppTest
12
{
13
/**//// <summary>
14
/// Form1 的摘要说明。
15
/// </summary>
16
public class Form1 : System.Windows.Forms.Form
17
{
18
private System.Windows.Forms.Button button1;
19
/**//// <summary>
20
/// 必需的设计器变量。
21
/// </summary>
22
private System.ComponentModel.Container components = null;
23
24
public Form1()
25
{
26
//
27
// Windows 窗体设计器支持所必需的
28
//
29
Debug.WriteLine ("初始化 Form1");
30
InitializeComponent();
31
32
//
33
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
34
//
35
}
36
37
/**//// <summary>
38
/// 清理所有正在使用的资源。
39
/// </summary>
40
protected override void Dispose( bool disposing )
41
{
42
Debug.WriteLine ("清理资源.");
43
if( disposing )
44
{
45
if (components != null)
46
{
47
components.Dispose();
48
}
49
}
50
base.Dispose( disposing );
51
}
52
53
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
54
/**//// <summary>
55
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
56
/// 此方法的内容。
57
/// </summary>
58
private void InitializeComponent()
59
{
60
System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
61
this.button1 = new System.Windows.Forms.Button();
62
this.SuspendLayout();
63
//
64
// button1
65
//
66
this.button1.Location = new System.Drawing.Point(128, 128);
67
this.button1.Name = "button1";
68
this.button1.TabIndex = 0;
69
this.button1.Text = "button1";
70
this.button1.Click += new System.EventHandler(this.button1_Click);
71
//
72
// Form1
73
//
74
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
75
this.ClientSize = new System.Drawing.Size(292, 266);
76
this.Controls.Add(this.button1);
77
this.Name = "Form1";
78
this.Text = ((string)(configurationAppSettings.GetValue("Form1.Text", typeof(string))));
79
this.ResumeLayout(false);
80
81
}
82
#endregion
83
84
/**//// <summary>
85
/// 应用程序的主入口点。
86
/// </summary>
87
[STAThread]
88
static void Main()
89
{
90
Debug.WriteLine ("启动应用程序.");
91
Application.Run(new Form1());
92
}
93
94
private void button1_Click(object sender, System.EventArgs e)
{
95
MessageBox.Show (ConfigurationSettings.AppSettings ["Port"].ToString ());
96
}
97
}
98
}
99

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


54


55

56

57

58

59



60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84


85

86

87

88

89



90

91

92

93

94



95

96

97

98

99
