我需要切换两个用户设置的节点,这些设置在运行时保存在app.config中,因为无法重命名密钥名称。 (WindowsFormsApplication2.Settings2到WindowsFormsApplication2.Settings1下的节点)
我尝试通过搜索网络找到解决方案并尝试使用下面的代码来执行此操作,但我收到错误“对象引用未设置为对象的实例”。
任何帮助都将不胜感激。
<?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="WindowsFormsApplication2.Settings2" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="WindowsFormsApplication2.Settings1" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="WindowsFormsApplication2.Properties.Settings" 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.6" />
</startup>
<userSettings>
<WindowsFormsApplication2.Settings1>
<setting name="Label1" serializeAs="String">
<value>Label1 FrenchName</value>
</setting>
<setting name="Label2" serializeAs="String">
<value>Label2 French Name</value>
</setting>
</WindowsFormsApplication2.Settings1>
<WindowsFormsApplication2.Settings2>
<setting name="Label1" serializeAs="String">
<value>Label1 English Name</value>
</setting>
<setting name="Label2" serializeAs="String">
<value>Label2 English Name</value>
</setting>
</WindowsFormsApplication2.Settings2>
<WindowsFormsApplication2.Properties.Settings>
<setting name="Label1" serializeAs="String">
<value>Test1</value>
</setting>
</WindowsFormsApplication2.Properties.Settings>
</userSettings>
</configuration>
源代码:
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configFile.AppSettings.Settings["Settings1"].Value = configFile.AppSettings.Settings["Settings2"].Value;