3
有什麼不對下面的代碼:我不能修改app.config文件
private static void UpdateAppSettings(string settingName, string settingValue)
{
if (settingName == null) throw new ArgumentNullException("settingName");
if (settingValue == null) throw new ArgumentNullException("settingValue");
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var appSettings = config.AppSettings;
var setting = appSettings.Settings[settingName];
setting.Value = settingValue;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("AppSettings");
}
我不知道爲什麼它不保存新的設置。它首先打開app.config文件。去Appsetting區後。然後它找到一個特定的密鑰settingName
並將值更改爲settingValue
。之後,它會保存文件並刷新它。它幾乎可行;最多config.Save(...)
。但之後我不知道會發生什麼。
你得到一個錯誤?文件沒有更新?更多的信息是完全需要*你的問題是什麼。 –
您是否在調試或發佈模式下嘗試過?據我記得這隻適用於釋放。 – Steve
檢查以下內容:http://stackoverflow.com/questions/758389/why-are-application-settings-read-only-in-app-config –