加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C#中读写app.config

发布时间:2020-12-15 21:51:01 所属栏目:百科 来源:网络整理
导读:我知道在这个论坛中有很多对app.config的引用,但我在这里发布这个问题,因为我认为我的问题非常直接. 我的app.config看起来像这样…… ?xml version="1.0"?configuration configSections sectionGroup name="userSettings" type="System.Configuration.UserSe
我知道在这个论坛中有很多对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,PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup,PublicKeyToken=b77a5c561934e089" >
            <section name="MySection.Properties.Settings" type="System.Configuration.ClientSettingsSection,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是在< / configSections>之后的配置文件顶部创建的.结束标记,如下所示:

<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup,PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
            <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup,PublicKeyToken=b77a5c561934e089" >
        <section name="MySection.Properties.Settings" type="System.Configuration.ClientSettingsSection,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键的重复条目(两者都用**突出显示,顶部用新的突出显示).

我是否使用错误的功能进行密钥写入?

解决方法

您的代码只允许访问< appSettings> < add key =“CurrentLogFile”value =“2”/>中的部分格式.
用于读/写< userSettings>和< applicationSettings>你应该使用标准的Settings.settings文件,它将按照以下格式进行输入< setting name =“CurrentLogFile”serializeAs =“String”> <值大于1< /值GT; < /设定>
项目中使用的变量名称将是字符串只读的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();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读