自动生成HTML工具 (part 6:config 文件读写)

本文介绍如何使用 .NET Framework 进行配置文件(config)的读取和写入操作。包括使用 ConfigurationManager 对象读取配置信息及更新配置文件的具体步骤。

config 文件 主要是用来记录 配置信息,一般是只读的,如果真的需要配置的时候才手动更改,这样的好处是 防止别人通过程序提供的方法更改配置;

网上有好多config 的例子,一般来说,读本身的配置文件 用 ConfiguationManager.AppSettings["key"] 就可以了

 #region 初始化参数
            
//任务列表 文件
            CreateHelp.Factory.TaskFactory.SaveFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["TaskList"]);

            CreateHelp.Factory.TaskFactory.timeWatch 
= int.Parse(ConfigurationManager.AppSettings["timeWatch"]);

            CreateHelp.Factory.TaskFactory.autoStart 
= bool.Parse(ConfigurationManager.AppSettings["AutoStart"]);
            
#endregion

 

的方法 在网上比较少,一般是通过写xml的方法 去做的,不过.net 2.0以后就提供了方法出来 写config文件了

             // Get the configuration file.
                System.Configuration.Configuration config =
                  ConfigurationManager.OpenExeConfiguration(
"AutoCreateHTML.exe");

                
// Add an Application Setting.
                config.AppSettings.Settings["timeWatch"].Value = CreateHelp.Factory.TaskFactory.timeWatch.ToString();

                
// Save the configuration file.
                config.Save(ConfigurationSaveMode.Modified);

                
// Force a reload of a changed section.
                ConfigurationManager.RefreshSection("appSettings");

ConfigurationManager.RefreshSection("appSettings") 是 刷新自己的config 文件,如果改其他的config文件,这个方法是没有用的。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值