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节点下的第一个节点)

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

转载于:https://www.cnblogs.com/de0319gh/archive/2010/06/25/1765288.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值