配置文件
<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
<configSections>
<section name="SQLConfiguration"type="ConfigurationDemo.SQLConfiguration,ConfigurationDemo"/>
<section name="AccountConfiguration"type="ConfigurationDemo.AccountConfiguration,ConfigurationDemo"/>
</configSections>
<SQLConfiguration type="MSSQL"connectionString="server=.;integrated security=sspi;database=Northwind"></SQLConfiguration>
<AccountConfiguration>
<users username="liunian"passWord="123456"></users>
</AccountConfiguration>
<system.net>
<mailSettings>
<smtp from="liunian@QQ.com">
<network />
</smtp>
</mailSettings>
</system.net>
</configuration>
第一种
classSQLConfiguration : ConfigurationSection
{
[ConfigurationPRoperty("type", IsRequired =true)]publicstringType
{get{returnthis["type"].ToString(); }set{this["type"] =value; }
}
[ConfigurationProperty("connectionString", IsRequired =true)]publicstringConnectionString
{get{returnthis["connectionString"].ToString(); }set{this["connectionString"] =value; }
}
}
SQLConfiguration sqlConfig = (SQLConfiguration)ConfigurationManager.GetSection("SQLConfiguration");
Console.WriteLine(sqlConfig.Type);
Console.WriteLine(sqlConfig.ConnectionString);
第二种
publicclassAccountConfiguration : ConfigurationSection
{
[ConfigurationProperty("users", IsRequired =true)]publicAccountSectionElement Users
{get{return(AccountSectionElement)this["users"]; }
}
}publicclassAccountSectionElement : ConfigurationElement
{
[ConfigurationProperty("username", IsRequired =true)]publicstringUserName
{get{returnthis["username"].ToString(); }set{this["username"] =value; }
}
[ConfigurationProperty("password", IsRequired =true)]publicstringPassword
{get{returnthis["password"].ToString(); }set{this["password"] =value; }
}
}
AccountConfiguration accountConfig = (AccountConfiguration)ConfigurationManager.GetSection("AccountConfiguration");
Console.WriteLine(accountConfig.Users.UserName);
Console.WriteLine(accountConfig.Users.Password);
第三种
Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
SmtpSection section= config.GetSection("system.net/mailSettings/smtp")asSmtpSection;
Console.WriteLine(section.From);
第四种
http://www.cnblogs.com/liunlls/p/config.html
第五种
ConfigurationManager.AppSettings
第六种
ConfigurationManager.ConnectionStrings
当然还有很多......