在winform的安装工具中,少不免有一些配置文件要放到app.config去,于是修改也是成了一种需求!
无论是修改web.config还是app.config,普遍方式都有两种,用net自带封装的类,或是自定义xml操作。
可参考之前的一篇:
这里用的,还是以xml方式操作,比竟类都写了,就顺路用上了。
这里的操作方式和webconfig的差不多一个样:
![](http://static.oschina.net/uploads/img/201406/05233503_olQl.gif)
![05233503_6kM0.gif](http://static.oschina.net/uploads/img/201406/05233503_6kM0.gif)
string appConfigPath = startPath + " /XXX.exe.config " ; WebConfigHelper appConfig = new WebConfigHelper(appConfigPath); if (appConfig.LoadIsOK) { WebConfigAppSetting appSetting = appConfig.AppSetting; if (appSetting != null ) { appSetting.Set( " SoftSetup_WinRARSystemPath " , txtSoftSetup_WinRARSystemPath.Text); appSetting.Set( " SoftSetup_IISPath " , txtSoftSetup_IISPath.Text.Replace(startPath, "" )); } if (appConfig.Save()) { ConfigurationManager.RefreshSection( " appSettings " ); MessageBox.Show( " 修改成功! " ); return ; } } MessageBox.Show( " 修改失败! " );
这里最值得一提的一句是:ConfigurationManager.RefreshSection("appSettings");
修改完app.config时,虽然是修改了文件,但运行在内存中的app.config却还没有修改.
所以你改完文件,再取值,还是内存中的旧值,因此修改完后,需要重新加载一下。
打完,收工!