vshost.exe.config是程序運行時的配置文本
exe.config是程序運行后會復制到vshost.exe.config
app.config是在vshost.exe.config和exe.config沒有情況起作用,從app.config復制到exe.config再復制到vshost.exe.config
寫配置文件都是寫到exe.config文件中了,app.config不會變化。
app.config只在exe.config丟失的情況下在開發環境中重新加載app.config,vshost.exe.config和exe.config會自動創建內容跟app.config一樣。
vshost.exe.config和app.config兩個文件可不要,但exe.config文件不可少。
網絡上有很多文章是講app.config讀寫方法的,可是事實上這個文件程序就不能改變它。
public static void SaveAppSettings(string aKey, string aValue) { // 創建配置文件對象 string file = System.Windows.Forms.Application.ExecutablePath; //此處修改為.vshost.exe.Config的內容 // System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(file); if (config.AppSettings.Settings[aKey] != null) { // 修改 config.AppSettings.Settings[aKey].Value = aValue; } else { // 添加 AppSettingsSection ass = (AppSettingsSection)config.GetSection("appSettings"); ass.Settings.Add(aKey, aValue); } // 保存修改 config.Save(ConfigurationSaveMode.Modified); // 強制重新載入配置文件的連接配置節 ConfigurationManager.RefreshSection("appSettings"); }