体验2.0的配置文件管理

感谢领导们大发慈悲,终于同意新项目采用.net 2.0来开发了!
其实,私下和2.0约会也不是一次两次了,每次都收获颇丰、回味无穷!呵呵!

今天有点时间,就拿出一些如君共享!

关于配置文件的管理,网络上已经有很多的介绍,不过都是讲讲道理,简单实现一下。对于多数初学者而言,依然很难体会到新版本对配置文件的操作到底好在哪里,我刚刚写了一个实现,就拿出来给初学者作为参考!同时代码中如果存在问题,希望大家批评指正!

TtSoft.Demo.Config.cs 实体

 1None.gifusing System;
 2None.gifusing System.Configuration;
 3None.gif
 4None.gifnamespace TtSoft.Demo.Config
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 7InBlock.gif    /// 配置实体类
 8InBlock.gif    /// 
 9InBlock.gif    /// 访问的配置的方法如下:
10InBlock.gif    /// using ThSoft.Demo.Config;
11InBlock.gif    /// string sServer = ItemConfigManager.ApplicationConfig.Server;
12InBlock.gif    /// 
13InBlock.gif    /// 修改代码如下:
14InBlock.gif    /// ItemConfigManager.ApplicationConfig.Server = "test";
15InBlock.gif    /// ItemConfigManager.Save();
16InBlock.gif    /// 此时,再次访问Server结果为"test"
17ExpandedSubBlockEnd.gif    /// </summary>

18InBlock.gif    public class ItemConfig : ConfigurationSection
19ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
20ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
21InBlock.gif        /// 网络通讯机所在服务器
22ExpandedSubBlockEnd.gif        /// </summary>

23InBlock.gif        [ConfigurationProperty("Server")]
24ExpandedSubBlockStart.gifContractedSubBlock.gif        public string Serverdot.gif{
25ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gif
26InBlock.gif                return this["Server"].ToString(); 
27ExpandedSubBlockEnd.gif            }

28ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{
29InBlock.gif                this["Server"= value; 
30ExpandedSubBlockEnd.gif            }

31ExpandedSubBlockEnd.gif        }

32InBlock.gif
33ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
34InBlock.gif        /// 网络通讯机所用端口
35ExpandedSubBlockEnd.gif        /// </summary>

36InBlock.gif        [ConfigurationProperty("Port")]
37ExpandedSubBlockStart.gifContractedSubBlock.gif        public string Portdot.gif{
38InBlock.gif            get
39ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
40InBlock.gif                return this["Port"].ToString();
41ExpandedSubBlockEnd.gif            }

42InBlock.gif            set
43ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
44InBlock.gif                this["Port"= value;
45ExpandedSubBlockEnd.gif            }

46ExpandedSubBlockEnd.gif        }

47ExpandedSubBlockEnd.gif    }

48ExpandedBlockEnd.gif}

49None.gif

TtSoft.Demo.Config.ItemConfigManager.cs 操作以上实体
 1None.gifusing System;
 2None.gifusing System.Configuration;
 3None.gif
 4None.gifnamespace TtSoft.Demo.Config
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 7InBlock.gif    /// 全局配置文件管理
 8ExpandedSubBlockEnd.gif    /// </summary>

 9InBlock.gif    public class ItemConfigManager
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
12InBlock.gif        /// ItemConfig实例
13ExpandedSubBlockEnd.gif        /// </summary>

14InBlock.gif        public static ItemConfig ApplicationConfig;
15InBlock.gif
16ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
17InBlock.gif        /// 配置文件管理者
18ExpandedSubBlockEnd.gif        /// </summary>

19InBlock.gif        static ItemConfigManager()
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            if (ApplicationConfig != null)
22InBlock.gif                return;
23InBlock.gif            ApplicationConfig = new ItemConfig();
24ExpandedSubBlockEnd.gif        }

25InBlock.gif
26ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
27InBlock.gif        /// 初始化,在Program.cs中调用!
28ExpandedSubBlockEnd.gif        /// </summary>

29InBlock.gif        public static void Init()
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
31InBlock.gif            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
32InBlock.gif            if (config.Sections["ItemConfig"== null)
33ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
34InBlock.gif                ItemConfig ic = new ItemConfig();
35InBlock.gif                //ic.Server = "192.168.0.120";
36InBlock.gif
37InBlock.gif
38InBlock.gif                //ic.Port = "9039";
39InBlock.gif                config.Sections.Add("ItemConfig", ic);
40InBlock.gif                config.Save(ConfigurationSaveMode.Full);
41InBlock.gif                ConfigurationManager.RefreshSection("ItemConfig");
42ExpandedSubBlockEnd.gif            }

43InBlock.gif            Load();
44ExpandedSubBlockEnd.gif        }

45InBlock.gif
46ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
47InBlock.gif        /// 载入配置!
48ExpandedSubBlockEnd.gif        /// </summary>

49InBlock.gif        public static void Load()
50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
51InBlock.gif            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
52InBlock.gif            if (config.Sections["ItemConfig"!= null)
53ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
54InBlock.gif                ItemConfig ic = (ItemConfig)config.Sections["ItemConfig"];
55InBlock.gif                ApplicationConfig.Server = ic.Server;
56InBlock.gif                ApplicationConfig.Port = ic.Port;
57InBlock.gif                ApplicationConfig.DBFileName = ic.DBFileName;
58ExpandedSubBlockEnd.gif            }

59ExpandedSubBlockEnd.gif        }

60InBlock.gif
61ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
62InBlock.gif        /// 将配置的修改信息保存!
63ExpandedSubBlockEnd.gif        /// </summary>

64InBlock.gif        public static void Save()
65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
66InBlock.gif            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
67InBlock.gif            if (config.GetSection("ItemConfig"!= null)
68ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
69InBlock.gif                config.Sections.Remove("ItemConfig");
70ExpandedSubBlockEnd.gif            }

71InBlock.gif
72InBlock.gif            config.Sections.Add("ItemConfig", ApplicationConfig);
73InBlock.gif            config.Save(ConfigurationSaveMode.Full);
74InBlock.gif            ConfigurationManager.RefreshSection("ItemConfig");
75ExpandedSubBlockEnd.gif        }

76ExpandedSubBlockEnd.gif    }

77ExpandedBlockEnd.gif}

78None.gif

使用举例:
 1None.gifusing System;
 2None.gifusing TtSoft.Demo.Config;
 3None.gif
 4None.gifnamespace TtSoft.Demo
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6InBlock.gif    static void Main()
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        ItemConfigManager.Init(); //初始化
 9InBlock.gif        Console.WriteLine("Old Server:{0}",ItemConfigManager.ApplicationConfig.Server); //访问属性节点
10InBlock.gif        ItemConfigManager.ApplicationConfig.Server = "server12";
11InBlock.gif        ItemConfigManager.Save(); //保存信息        
12InBlock.gif        Console.WriteLine("New Server:{0}",ItemConfigManager.ApplicationConfig.Server); //访问属性节点
13ExpandedSubBlockEnd.gif    }

14ExpandedBlockEnd.gif}

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

转载于:https://www.cnblogs.com/minzhu/archive/2006/10/31/545751.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值