我正在开发使用.NET框架4.6.1一个C#WPF MVVM应用程序,我在App.config中自定义栏目:修改定制的app.config配置部分,并将其保存
<configuration>
<configSections>
<section name="SpeedSection" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<SpeedSection>
<add key="PrinterSpeed" value="150" />
<add key="CameraSpeed" value="150" />
</SpeedSection>
</configuration>
我想从我的应用修改PrinterSpeed
和CameraSpeed
。我试过这段代码:
static void AddUpdateAppSettings(string key, string value)
{
try
{
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;
if (settings[key] == null)
{
settings.Add(key, value);
}
else
{
settings[key].Value = value;
}
configFile.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
}
catch (ConfigurationErrorsException)
{
Console.WriteLine("Error writing app settings");
}
}
但它不起作用,因为我没有修改AppSettings
部分。
如何修改这些值?
我加入这个作为一个评论,因为它不是一个真正的回答你的问题,但可能会帮助你。您是否研究过非常尴尬且令人沮丧的Microsoft解决方案的替代方案。有更好的应用程序配置选择,例如NuGet Urchin软件包。 – bikeman868