尝试存储并将某些用户definde值读取到* .config文件 从文件中读取的代码
public int GetVolumeFromConfigFile()
{
return Convert.ToInt32(ConfigurationManager.AppSettings["Volume"]);
}
完美无缺
尝试使用此
更新现有密钥 public int SetVolumeFromConfigFile()
{
ConfigurationManager.AppSettings["Volume"] = "10";
}
收到错误 - 文件仅供阅读,更改为:
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Add(label.Text.ToString(), box.Value.ToString());
config.Save(ConfigurationSaveMode.Minimal);
没有错误,但文件中没有任何变化。
我的* .config文件是:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="PlayDemo.SettingsPlayIt" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<userSettings>
<PlayDemo.SettingsPlayIt>
<setting name="Volume" serializeAs="String">
</setting>
</PlayDemo.SettingsPlayIt>
</userSettings>
试着去了解我错在哪里
答案 0 :(得分:3)
这是
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);
config.AppSettings.Settings["Volume"].Value = "10";
config.Save(ConfigurationSaveMode.Modified);
这不会在Visual Studio中更新项目中的App.config,而是在项目的bin文件夹中更新&#34; ExecutableName&#34; .config。