如何在C#Winforms中的app.config中保存配置

mon*_*oys 8 c# configuration app-config

有人能举例说明如何使用C#和WinForms 在app.config中保存键/值吗?

Edw*_*Tai 22

ASP.NET中:

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);
Run Code Online (Sandbox Code Playgroud)

WinForms中:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);
Run Code Online (Sandbox Code Playgroud)

  • 如果那个密钥已经存在怎么办?它会取代它吗? (3认同)
  • @monkey_boys:当从VS运行例如win form应用程序时,要查找的相关.config文件是:<我的应用程序> .vshost.exe.config,所以如果你在查找<我的应用程序> .exe.config然后你没有看到任何变化. (2认同)