How can I read/write app.config settings at runtime without using user settings?

后端 未结 3 1525
长发绾君心
长发绾君心 2020-11-28 09:16

I\'m looking for a way to store application or machine level settings that can be written to at runtime using Application Settings. User settings allow read/write but applic

3条回答
  •  暖寄归人
    2020-11-28 09:41

    Well, I haven't yet wanted to change application settings at runtime (that's what I use user settings for), but what I have been able to do is write application settings at install time. I imagine that a similar approach might work at runtime. You could try it out since there don't seem to be any other propsed solutions ATM.

        exePath = Path.Combine( exePath, "MyApp.exe" );
        Configuration config = ConfigurationManager.OpenExeConfiguration( exePath );
        var setting = config.AppSettings.Settings[SettingKey];
        if (setting != null)
        {
            setting.Value = newValue;
        }
        else
        {
            config.AppSettings.Settings.Add( SettingKey, newValue);
        }
    
        config.Save();
    

    Hope that helps!

提交回复
热议问题