【发布时间】:2022-01-18 23:10:09
【问题描述】:
我对此完全束手无策,基本上已经尝试了所有方法。我也没有看到任何关于这样做的现有 stackoverflow 线程。
我的 C# 项目有一个 app.config 文件,它存储了用户可以创建和添加到的服务器列表。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="servers" type="System.Configuration.AppSettingsSection" />
</configSections>
<servers>
<add key="server" value="678,true,true"/>
</servers>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
<appSettings>
<add key="nightmode" value="Dark"/>
<add key="theme" value="Red"/>
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="ControlzEx" publicKeyToken="69f1c32f803d307e" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
((NameValueCollection)ConfigurationManager.GetSection("servers")).Add("server", $"{port}," + $"{ (dialogResult1 == MessageDialogResult.Affirmative) },{dialogResult2 == MessageDialogResult.Affirmative}");
当用户在“服务器”部分添加新密钥时,会引发以下异常。
System.Configuration.ConfigurationErrorsException: 'The configuration is read only.'
我很困惑为什么会这样
【问题讨论】:
-
这是因为您的应用配置是只读的。正如错误消息所说。 social.technet.microsoft.com/wiki/contents/articles/…
-
你为什么要在运行时写入配置文件...?
-
@mm8 那么你有什么建议?我需要等待将设置保存到配置文件中
-
@devlincarnate 可能会提供原因,但我不确定什么是解决此问题的正确方法。