Web.Config or a custom segment app.Config example configSections

Web.Config or a custom segment app.Config example configSections

I feel more and more, app.Config or web.Config operating parameters to configure the application system configuration file directly, than they do a xml configuration file, is much more simple and convenient. Both profiles not only common appSettings connectionStrings and, given the definition of database connections or access methods common name / value table, but also provides a custom configSections segment, can define block elements, the extension of a segment appSettings Features. The following is a specific application example.

 

1, the configuration file (Web.config or App.config)

 

<? xml Version = "1.0" encoding = "UTF-8"?>
<the Configuration>

  <configSections> <-! This element must precede the appSettings ->
      <sectionGroup name = "Units">
          <Section name = "defense portion "type =" System.Configuration.NameValueSectionHandler "/>
          <sectionTop name =" MPS "type =" System.Configuration.NameValueSectionHandler "/>
      </ sectionGroup>
  </ configSections>

  <Units>
      <DOD>
          <the Add Key = value = "Defense Minister" "Minister name" />
          <the Add Key = "Deputy Minister" value = "Minister of Defense" />
    </ Department of Defense>


    <Ministry of Public Security>
        </ value = "Minister of Public Security" add key = "minister name">
        <the Add Key = "Deputy Minister" value = "Ministry of Public Security Minister" />
    </ Ministry of Public Security>
  </ Units>

  <appSettings >
      <the Add Key = "A1" value = "A1Value" />
      <the Add Key = "A2" value = "A2Value" />
  </ the appSettings>

</ Configuration>

 

It should be noted:

  1. In configSections, you must first define the group name from the group of elements defined segments, namely "Units", and then define two segments Units of the "Department of Defense" and "Ministry of Public Security." Obviously, you can define a plurality of segments, and groups Units.
  2. In the configuration file, configSections element must precede the appSettings element, otherwise it will report an access error.

 

2, access node custom configuration

 

      NameValueCollection sections = (NameValueCollection)ConfigurationManager.GetSection("Units/国防部");
      if (sections != null)
      {
           for(int k = 0; k < sections.Keys.Count; k++)
           {
               listBox1.Items.Add(sections.Keys[k] + "  " + sections[k]);
           }
      }

      listBox1.Items.Add(ConfigurationManager.AppSettings["A1"]);
      listBox1.Items.Add(ConfigurationManager.AppSettings["A2"]);

 

在代码中,可以直接使用sections["部长名"]、sections["副部长"]的形式获取key的value值,也可以GetSection("Units/公安部")获取"公安部"段的key和value值。

 

需要指出,在VS 2005的项目中

  • 必须添加引用程序集 System.configuration;
  • 在名称空间添加 using System.Configuration。

本文配置文件和程序代码在VC# 2005和.NET 2.0环境下的窗体项目中编译通过。可以看出,访问自定义段和appSettings段的访问的几乎没有差别,仍然是简洁与直接的。


转载于:https://www.cnblogs.com/kevinGao/p/4188740.html

Guess you like

Origin blog.csdn.net/weixin_33860528/article/details/93767137