我知道这个论坛有很多参考资料app.config
,但我在这里发布这个问题是因为我认为我的问题非常直接。
我的app.config
长相是这样的...
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="MySection.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MySection.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<userSettings>
<MySection.Properties.Settings>
<setting name="DEVICE_ID_VERSION" serializeAs="String">
<value>1.0.0.0</value>
</setting>
<setting name="DEVICE_ID_ID" serializeAs="String">
<value>0000 0001</value>
</setting>
</MySection.Properties.Settings>
</userSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="PInvoke" publicKeyToken="83380E73B2486719" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.19.0" newVersion="3.0.19.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Utilities" publicKeyToken="83380E73B2486719" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.18.0" newVersion="3.0.18.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<applicationSettings>
<MySection.Properties.Settings>
<setting name="CurrentLogFile" serializeAs="String">
<value>1</value>
</setting>
</MySection.Properties.Settings>
</applicationSettings>
</configuration>
我已将设计器页面中的新CurrentLogFile
密钥添加为密钥。我需要在应用程序启动时读取它,并在运行时日志文件编号发生变化时写入它。Settings.Settings
Application
我编写的以下代码无法重写Setting
密钥。它在配置文件中创建一个全新的条目:
int curLogFile = Settings.Default.CurrentLogFile;
curLogFile = curLogFile +1;
// Update the new log file number to the config "CurrentLogFile" key
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
bool bReadOnly = config.AppSettings.Settings.IsReadOnly();
config.AppSettings.Settings.Remove("CurrentLogFile");
config.AppSettings.Settings.Add("CurrentLogFile", curLogFile.ToString());
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
Settings.Default.Reload();
新CurrentLogFile
的创建在配置文件顶部的</configSections>
结束标记之后,如下所示:
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="MySection.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MySection.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
**<appSettings>
<add key="CurrentLogFile" value="2" />
</appSettings>**
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<userSettings>
<MySection.Properties.Settings>
<setting name="DEVICE_ID_VERSION" serializeAs="String">
<value>1.0.0.0</value>
</setting>
</MySection.Properties.Settings>
</userSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Utilities" publicKeyToken="83380E73B2486719" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.18.0" newVersion="3.0.18.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<applicationSettings>
<MySection.Properties.Settings>
**<setting name="CurrentLogFile" serializeAs="String">
<value>1</value>
</setting>**
</MySection.Properties.Settings>
</applicationSettings>
</configuration>
这会创建CurrentLogFile
密钥的重复条目(都用 突出显示**
,顶部的新条目)。
我是否使用了错误的功能来编写密钥?