简体   繁体   中英

reading and writing app.config in C#

I understand there are lot of references to app.config in this forum, but I am posting this question here as I think my question is very direct.

My app.config looks like this...

<?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>

I have added the new CurrentLogFile key from the Settings.Settings designer page as an Application key. I need to read this on application startup and write to it when there is a change in the logfile number at runtime.

The following code that I wrote is unable to re-write the Setting key. It creates an entirely new entry in the config file:

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();

The new CurrentLogFile is created at the top of the config file just after the </configSections> closing tag, as shown below:

<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>

This creates duplicate entries of the CurrentLogFile key (both highlighted with ** , the new one at the top).

Am I using the wrong function for key writing?

Your code allows access only to the <appSettings> section in <add key="CurrentLogFile" value="2" /> format. For read/write <userSettings> and <applicationSettings> you should use standard Settings.settings file, that will do the entries in the following format <setting name="CurrentLogFile" serializeAs="String"> <value>1</value> </setting> The variable name used in the project will be string readonly YourNamespace.Properties.Settings.Default.CurrentLogFile , because you put it in application scope. User scope allows rewriting:

<userSettings>
    <MySection.Properties.Settings>
        <setting name="DEVICE_ID_VERSION" serializeAs="String">
           <value>1.0.0.0</value>
        </setting>
    </MySection.Properties.Settings>
</userSettings>

MySection.Properties.Settings.Default.DEVICE_ID_VERSION = "1.5.0.0";
MySection.Properties.Settings.Default.Save();

您可以对此Config文件使用XML读取和写入方法,因为app.config和XML的基本体系结构是相同的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM