在winform的安裝工具中,少不免有一些配置檔案要放到app.config去,于是修改也是成了一種需求!
無論是修改web.config還是app.config,普遍方式都有兩種,用net自帶封裝的類,或是自定義xml操作。
這裡用的,還是以xml方式操作,比竟類都寫了,就順路用上了。
這裡的操作方式和webconfig的差不多一個樣:
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卻還沒有修改.
是以你改完檔案,再取值,還是記憶體中的舊值,是以修改完後,需要重新加載一下。
打完,收工!