update app.config file programmatically with ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

后端 未结 2 1269
心在旅途
心在旅途 2020-12-01 11:21

update app.config file programmatically with

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

2条回答
  •  暖寄归人
    2020-12-01 12:05

    You can use the following code:

    private void UpdateConfig(string key, string value, string fileName)
    {
        var configFile = ConfigurationManager.OpenExeConfiguration(fileName);
        configFile.AppSettings.Settings[key].Value = value;
    
        configFile.Save();
    }
    

    Where: fileName is the full path + application name (c:\project\application.exe)

    In your case, change the AppSetting by Sections:

    configFile.Sections["nhibernateSettings"]
    

提交回复
热议问题