• DotNet中配置文件的使用(一)


    在.Net平台下的配置文件主要使用在Web开发和桌面开发中,对应的配置文件类型也不一样,Web中为Web.config文件,但是在桌面应用中为App.config文件。

    当然使用上也稍微有些区别,下文将会阐述。

    配置文件结构大致如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      
      <!--自定义配置节点-->
      <configSections>
        <sectionGroup name="OwnSectionA">
          <section name="AAA" type="AppLibrary.Configuration.MySelfInfoHandle,AppLibrary,Version=1.0.0.0"/>
          <section name="BBB" type="System.Configuration.SingleTagSectionHandler"/>
    
        </sectionGroup>
        <sectionGroup name="OwnSectionB">
          <section name="aaa" type="System.Configuration.SingleTagSectionHandler"/>
          <section name="bbb" type="System.Configuration.SingleTagSectionHandler"/>
        </sectionGroup>
      </configSections>
    
      <!--自定义节点内容区域-->
      <OwnSectionA>
        <AAA>
          <add key="name" value="huchen's homepage"/>
          <add key="version" value="1.0"/>
        </AAA>
      </OwnSectionA>
      
      <!--读取修改配置节点-->
      <appSettings>
        <clear/>
        <add key ="Access" value="/Date/mvp.accdb"/>
        <add key="Sql" value="null"/>
      </appSettings>
      
    </configuration>


    对于配置文件的使用主要分两种类型:

    使用之前请确保项目已经引用了System.configuration程序集。

    (一):系统预定义的配置节点(大家所熟悉的appSettings节点以及Web.config中connectionStrings节点

    对于connectionStrings配置节点的使用简单介绍,因为大家太熟悉了,主要使用方法看如下代码:

     <connectionStrings>
        <add name="SQLConn" connectionString="data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=nORTHWIND"
                providerName="System.Data.SqlClient" />
        <add name="SQLConnB" connectionString="server=.\SQLEXPRESS;database=Northwind;uid=sa;pwd=de"
              providerName="System.Data.SqlClient"/>
      </connectionStrings>
    

    System.Configuration.ConfigurationManager.ConnectionStrings["SQLConn"].ToString()               或者System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SQLConn"].ToString() 即可获取配置文件中的值

    对于appSettings配置节点的使用读取与connectionStrings节点类似,只不过使用WebConfigurationManager.AppSettings和ConfigurationManager.AppSettings。

    下面主要说一下appSettings节点的增加,修改和删除,当然进行修改操作要确保用户具有修改权限。直接看代码:

    Web中使用:

               System.Configuration.Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
              
    System.Configuration.AppSettingsSection app = configuration.AppSettings; 

              
    app.Settings.Add(key, value); //增加

              
    app.Settings[key].Value=value; //修改

              
    app.Settings.Remove(key);  //删除

              
    configuration.Save(System.Configuration.ConfigurationSaveMode.Modified);

    WinForm中使用:

               Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 

              
    AppSettingsSection app = config.AppSettings; 

              
    app.Settings.Add(key, value); //增加
              
    app.Settings[key].Value=value; //修改

              
    app.Settings.Remove(key);  //删除

              
    config.Save(ConfigurationSaveMode.Modified);

    实时读取小技巧:

    ConfigurationManager.AppSettings 返回的是System.Collections.Specialized.NameValueCollection 表示可以通过索引访问的关联 String 键和 String 值的集合。

    关于Winform下实时读取技巧代码段(通过在读取并展现出来就不需要重新加载了,刚修改过的节点会立刻呈现出来):

                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
               
    List<string> dir = new System.Collections.Generic.List<string>();
               
    AppSettingsSection app = config.AppSettings;
               
               
    string[] str = app.Settings.AllKeys;

               
    for (int i = 0; i < str.Length; i++)
               
    {
                   
    dir.Add(str[i] + "||" + app.Settings[str[i]].Value);
               
    }

               
    this.listBox1.DataSource =dir;

    (二):用户自定义的配置节点(如果有用户自定义的配置节点,则必须是configuration节点下的第一个节点)

               关于用户自定义的配置节点的使用(待续)

  • 相关阅读:
    函数指针和指针函数和回调函数以及函数指针数组
    C语言中的结构体,结构体数组
    linux中的shell脚本编程
    回车和换行在linux下和windows下
    内存的段式管理和页式管理,逻辑地址-虚拟地址-物理地址
    [CSAPP-II] 链接[符号解析和重定位] 静态链接 动态链接 动态链接接口
    c语言中函数调用的本质从汇编角度分析
    运算符优先级
    Redis实战经验及使用场景
    RESTful API 设计最佳实践【转】
  • 原文地址:https://www.cnblogs.com/de0319gh/p/1765288.html
Copyright © 2020-2023  润新知