. Discussion on configuration and configurationmanager/appsettings and configsections in net environment

Source: Internet
Author: User

There are a number of details to note about the configuration file settings.

A in general, the configuration document defaults to the value in the AppSettings property under it.

The following is the common VB Code implementation method:

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 for customer-defined sections must be placed in configsections when reading these section procedures.

It needs to be judged according to the App. Config file path.

(1) for the default path, the app. Config can be used directly

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

System.Collections.Hashtable)

Dim rtnvalue = NB (strkey)

(2) for a man-defined path, that is, the case in a

Dim myparamssection as ConfigurationSection = Configcache.getsection ("Usersection")

Dim myparamssectionrawxml As String = MyParamsSection.SectionInformation.GetRawXml () Dim sectionxmldoc As Xml.xmldo Cument = new Xml.xmldocument () sectionxmldoc.load (new StringReader (myparamssectionrawxml)) Dim handler as Na Mevaluesectionhandler = New NameValueSectionHandler () Dim handlercreatedcollection as Specialized.namevaluecollecti On handlercreatedcollection = CType (handler. Create (Nothing, Nothing, sectionxmldoc.documentelement), specialized.namevaluecollection) If not Handlercreatedcoll Ection.        Allkeys.contains (key) then return Defaultdata Else return handlercreatedcollection (key) End If

The configuration file needs to be increased

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

Of course, the same effect can be achieved if the control is read directly from the XML.

If you have any doubts, please read the Microsoft source code directly

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

. Discussion on configuration and configurationmanager/appsettings and configsections in net environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.