Я понимаю, что в этом форуме есть много ссылок на app.config
, но я размещаю этот вопрос здесь, поскольку я думаю, что мой вопрос очень прямой.чтение и письмо app.config в C#
Мои 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
(оба выделены **
, а новый - вверху).
Я использую неправильную функцию для написания ключа?
Properties.Settings.Default такого же, как области файла конфигурации. config.AppSettings.Settings - . 2 разных области, поэтому вы читаете значение 1 из и сохраняете его значение + 1 (2) в –
Wolf5