感谢领导们大发慈悲,终于同意新项目采用.net 2.0来开发了!
其实,私下和2.0约会也不是一次两次了,每次都收获颇丰、回味无穷!呵呵!
今天有点时间,就拿出一些如君共享!
关于配置文件的管理,网络上已经有很多的介绍,不过都是讲讲道理,简单实现一下。对于多数初学者而言,依然很难体会到新版本对配置文件的操作到底好在哪里,我刚刚写了一个实现,就拿出来给初学者作为参考!同时代码中如果存在问题,希望大家批评指正!
TtSoft.Demo.Config.cs 实体
1
using System;
2
using System.Configuration;
3
4
namespace TtSoft.Demo.Config
5

{
6
/**//// <summary>
7
/// 配置实体类
8
///
9
/// 访问的配置的方法如下:
10
/// using ThSoft.Demo.Config;
11
/// string sServer = ItemConfigManager.ApplicationConfig.Server;
12
///
13
/// 修改代码如下:
14
/// ItemConfigManager.ApplicationConfig.Server = "test";
15
/// ItemConfigManager.Save();
16
/// 此时,再次访问Server结果为"test"
17
/// </summary>
18
public class ItemConfig : ConfigurationSection
19
{
20
/**//// <summary>
21
/// 网络通讯机所在服务器
22
/// </summary>
23
[ConfigurationProperty("Server")]
24
public string Server
{
25
get
{
26
return this["Server"].ToString();
27
}
28
set
{
29
this["Server"] = value;
30
}
31
}
32
33
/**//// <summary>
34
/// 网络通讯机所用端口
35
/// </summary>
36
[ConfigurationProperty("Port")]
37
public string Port
{
38
get
39
{
40
return this["Port"].ToString();
41
}
42
set
43
{
44
this["Port"] = value;
45
}
46
}
47
}
48
}
49

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

TtSoft.Demo.Config.ItemConfigManager.cs 操作以上实体
1
using System;
2
using System.Configuration;
3
4
namespace TtSoft.Demo.Config
5

{
6
/**//// <summary>
7
/// 全局配置文件管理
8
/// </summary>
9
public class ItemConfigManager
10
{
11
/**//// <summary>
12
/// ItemConfig实例
13
/// </summary>
14
public static ItemConfig ApplicationConfig;
15
16
/**//// <summary>
17
/// 配置文件管理者
18
/// </summary>
19
static ItemConfigManager()
20
{
21
if (ApplicationConfig != null)
22
return;
23
ApplicationConfig = new ItemConfig();
24
}
25
26
/**//// <summary>
27
/// 初始化,在Program.cs中调用!
28
/// </summary>
29
public static void Init()
30
{
31
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
32
if (config.Sections["ItemConfig"] == null)
33
{
34
ItemConfig ic = new ItemConfig();
35
//ic.Server = "192.168.0.120";
36
37
38
//ic.Port = "9039";
39
config.Sections.Add("ItemConfig", ic);
40
config.Save(ConfigurationSaveMode.Full);
41
ConfigurationManager.RefreshSection("ItemConfig");
42
}
43
Load();
44
}
45
46
/**//// <summary>
47
/// 载入配置!
48
/// </summary>
49
public static void Load()
50
{
51
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
52
if (config.Sections["ItemConfig"] != null)
53
{
54
ItemConfig ic = (ItemConfig)config.Sections["ItemConfig"];
55
ApplicationConfig.Server = ic.Server;
56
ApplicationConfig.Port = ic.Port;
57
ApplicationConfig.DBFileName = ic.DBFileName;
58
}
59
}
60
61
/**//// <summary>
62
/// 将配置的修改信息保存!
63
/// </summary>
64
public static void Save()
65
{
66
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
67
if (config.GetSection("ItemConfig") != null)
68
{
69
config.Sections.Remove("ItemConfig");
70
}
71
72
config.Sections.Add("ItemConfig", ApplicationConfig);
73
config.Save(ConfigurationSaveMode.Full);
74
ConfigurationManager.RefreshSection("ItemConfig");
75
}
76
}
77
}
78

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

使用举例:
1
using System;
2
using TtSoft.Demo.Config;
3
4
namespace TtSoft.Demo
5

{
6
static void Main()
7
{
8
ItemConfigManager.Init(); //初始化
9
Console.WriteLine("Old Server:{0}",ItemConfigManager.ApplicationConfig.Server); //访问属性节点
10
ItemConfigManager.ApplicationConfig.Server = "server12";
11
ItemConfigManager.Save(); //保存信息
12
Console.WriteLine("New Server:{0}",ItemConfigManager.ApplicationConfig.Server); //访问属性节点
13
}
14
}

2

3

4

5



6

7



8

9

10

11

12

13

14

以上可以看出,vs2005的配置文件管理不但实现了可读可写,而且代码写起来还是比以前更简单,呵呵!真乃快速开发人员之福也!!!