我正在開發使用.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