【问题标题】:reading and writing app.config in C#在 C# 中读写 app.config
【发布时间】:2013-05-10 05:33:28
【问题描述】:

我知道这个论坛中有很多对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>

我已将 Settings.Settings 设计器页面中的新 CurrentLogFile 键添加为 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创建在配置文件顶部&lt;/configSections&gt;结束标记之后,如下所示:

<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) 存储在

标签: c# app-config


【解决方案1】:

您的代码只允许访问&lt;add key="CurrentLogFile" value="2" /&gt; 格式的&lt;appSettings&gt; 部分。 对于读/写 &lt;userSettings&gt;&lt;applicationSettings&gt;,您应该使用标准 Settings.settings 文件,该文件将以以下格式输入 &lt;setting name="CurrentLogFile" serializeAs="String"&gt; &lt;value&gt;1&lt;/value&gt; &lt;/setting&gt; 项目中使用的变量名将是string readonly YourNamespace.Properties.Settings.Default.CurrentLogFile,因为您将其置于应用程序范围内。用户范围允许重写:

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

【讨论】:

  • 使用 userSettings 会将更改存储在 %APPDATA% 下的其他位置,但您可以编写自己的 SettingsProvider 类,该类可能会修改原始配置文件,而不是将更改写入其他位置。
【解决方案2】:

您可以对这个Config文件使用XML读写方式 因为 app.config 和 XML 的基本架构是一样的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-23
    • 2015-08-11
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    相关资源
    最近更新 更多