/// <summary> /// 保存appSetting /// </summary> /// <param name="key">appSetting的KEY值</param> /// <param name="value">appSetting的Value值</param> private static void SetAppSetting(string key, string value) { // 創建配置文件對象 System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (config.AppSettings.Settings[key] != null) { // 修改 config.AppSettings.Settings[key].Value = value; } else { // 添加 AppSettingsSection ass = (AppSettingsSection)config.GetSection("appSettings"); ass.Settings.Add(key, value); } // 保存修改 config.Save(ConfigurationSaveMode.Modified); // 強制重新載入配置文件的連接配置節 ConfigurationManager.RefreshSection("appSettings"); }