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

后端 未结 3 1523
长发绾君心
长发绾君心 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:28

    WPF applications are able to access the app.config file just like WinForms apps through the

    ConfigurationManager.OpenExeConfiguration()
    

    method. The trick is to have the values you want to access in the AppSettings tag of your App.config file (also available in WPF applications).

    The trick to all of this is to make sure to call the following methods when you're done modifying your properties:

    MyConfig.Save(ConfigurationSaveMode.Modified)
    ConfigurationManager.RefreshSection("appSettings")
    

    I wrote a complete "how to" on this a little while back that explains it all here.

提交回复
热议问题