c# - 在 C# 中读写 app.config

标签 c# app-config

我知道有很多对 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 的 key 设计师页面作为 Application key 。我需要在应用程序启动时读取它,并在运行时日志文件编号发生变化时写入它。

我编写的以下代码无法重写 Setting key 。它在配置文件中创建一个全新的条目:

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 的重复条目键(都用 ** 突出显示,新的在顶部)。

我是否使用了错误的函数来编写 key ?

最佳答案

您的代码只允许访问 <appSettings> <add key="CurrentLogFile" value="2" /> 中的部分格式。 用于读/写 <userSettings><applicationSettings>您应该使用标准的 Settings.settings 文件,它将以以下格式执行条目 <setting name="CurrentLogFile" serializeAs="String"> <value>1</value> </setting> 项目中使用的变量名称将为 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();

关于c# - 在 C# 中读写 app.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16568549/

相关文章:

c# - 正则表达式解析 "^[\0\8]*$"- 无法识别的转义序列\8

c# - 如何使用默认覆盖处理异步和等待

c# - 在 C# 中将 Word 预定义样式应用于文本

c# - 在自定义部分中获取同一键的多个实例

c# - 将配置文件的内容读入与之关联的 dll

c# - 从 Request.Url.AbsolutePath 获取应用程序相对 url

c# - 为什么单击 ZedGraph 时我的图表上会出现垂直线?

c# - 如何确定 "ConfigurationElement"在 "ConfigurationSection"自定义配置设置上可用

.net - app.config 是什么时候创建的,什么时候是 app.exe.config 和有什么区别

c# - 如何在 C# Winforms 的 app.config 中保存配置