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
// 任务列表 文件
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 " );
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文件,这个方法是没有用的。