.NET环境下Configuration 与ConfigurationManager/ AppSettings 与 ConfigSections探讨

本文详细介绍了配置文件的两种读取方法:一是默认使用AppSettings属性中的值;二是针对自定义Section的读取过程,包括如何根据App.Config文件路径进行判断,并提供了具体的VB.NET代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于配置文件的设置,读取有不少细节需要注意的。

A  一般情况下,配置文档会默认使用其下的AppSettings属性中的值。

 以下为普通的VB代码实现方式: 

        Dim exeFileMap As ExeConfigurationFileMap = New ExeConfigurationFileMap()

        exeFileMap.ExeConfigFilename = configFilePath

        Dim configCache As Configuration 
= ConfigurationManager.OpenMappedExeConfiguration(exeFileMap, ConfigurationUserLevel.None)  

        Dim rtnValue = configCache.AppSettings.Settings(strKey).Value

 

B 对于客户自定义的Section必须放在configSections中,在读取这些Section过程时。

需要根据App.Config文件路径,进行判断。

    (1) 对于默认路径下App.Config, 可以直接用

     Dim nb As System.Collections.Hashtable
= CType(System.Configuration.ConfigurationManager.GetSection("MajorCommands"), _

System.Collections.Hashtable)

     Dim rtnValue = nb(strkey) 

    (2) 对于人为定义的路径,即A中的情况

        Dim myParamsSection As ConfigurationSection = configCache.GetSection("USERSECTION")

        Dim myParamsSectionRawXml As String = myParamsSection.SectionInformation.GetRawXml()
        Dim sectionXmlDoc As Xml.XmlDocument = New Xml.XmlDocument()
        sectionXmlDoc.Load(New StringReader(myParamsSectionRawXml))
        Dim handler As NameValueSectionHandler = New NameValueSectionHandler()
        Dim handlerCreatedCollection As Specialized.NameValueCollection
        handlerCreatedCollection = CType(handler.Create(Nothing, Nothing, sectionXmlDoc.DocumentElement), Specialized.NameValueCollection)
        If Not handlerCreatedCollection.AllKeys.Contains(key) Then
            Return defaultData
        Else
            Return handlerCreatedCollection(key)
        End If

 

 配置文件中需要增加

  <configSections>
    <section name ="USERSECTION"
             type ="System.Configuration.DictionarySectionHandler" />
  </configSections>

 <appSettings></appSettings>

 

 <USERSECTION>

 

    <!--Customize-->
    <add key ="key1" value ="SHINSHO"/>
    <add key ="key2" value ="SOJITZ"/>
    <add key ="key3" value ="SUMITOMO"/>
    <add key ="key4" value ="MITSUBISHI  ELECTRIC"/>
    <add key ="key5" value ="MITSUBISHI"/>
  </USERSECTION>

         

 当然,如果直接用XML读取控件,来实现也能达到同样的效果。

如果再有疑惑,请直接阅读微软的源代码

 https://referencesource.microsoft.com/#System.Configuration 

转载于:https://www.cnblogs.com/tomclock/p/7215880.html

<?xml version=“1.0” encoding=“utf-8”?> <configuration> <configSections> <section name=“connectionSettings” type=“System.Configuration.AppSettingsSection” /> <section name=“communicationSettings” type=“System.Configuration.AppSettingsSection” /> <section name=“packageSettings” type=“System.Configuration.AppSettingsSection” /> <section name=“sessionSettings” type=“System.Configuration.AppSettingsSection” /> <section name=“analyticsSettings” type=“System.Configuration.AppSettingsSection” /> <section name=“webProxySettings” type=“System.Configuration.AppSettingsSection” /> <section name=“robotCacheSettings” type=“System.Configuration.AppSettingsSection” /> <section name=“robotJsSettings” type=“System.Configuration.AppSettingsSection” /> </configSections> <connectionSettings> <add key=“defaultServiceUrl” value=“https://cloud.uipath.com” /> </connectionSettings> <communicationSettings> <add key=“maxMessageSizeInMegabytes” value=“1” /> <add key=“installPackageTimeout” value=“00:20:00” /> <add key=“requestTimeout” value=“00:00:40” /> </communicationSettings> <packageSettings> <add key=“disableSecureXaml” value=“False” /> </packageSettings> <sessionSettings /> <analyticsSettings> <add key=“Telemetry.Enabled” value=“True” /> </analyticsSettings> <webProxySettings /> <robotCacheSettings /> <robotJsSettings /> </configuration>C#如何读取这个配置文件里的内容
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值