Configuration file
<?xml version="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= "[email protected]" >
<network/>
</smtp>
</mailSettings>
</system.net></configuration>
First Kind
classsqlconfiguration:configurationsection {[ConfigurationProperty ("type", isrequired =true)] Public stringType {Get{return This["type"]. ToString (); } Set{ This["type"] =value;} } [ConfigurationProperty ("connectionString", isrequired =true)] Public stringConnectionString {Get{return This["connectionString"]. ToString (); } Set{ This["connectionString"] =value;} } }
Sqlconfiguration sqlconfig = (sqlconfiguration) configurationmanager.getsection ("sqlconfiguration "); Console.WriteLine (sqlconfig.type); Console.WriteLine (sqlconfig.connectionstring);
The second Kind
Public classaccountconfiguration:configurationsection {[ConfigurationProperty ("Users", isrequired =true)] Publicaccountsectionelement Users {Get{return(accountsectionelement) This["Users"]; } } } Public classaccountsectionelement:configurationelement {[ConfigurationProperty ("username", isrequired =true)] Public stringUserName {Get{return This["username"]. ToString (); } Set{ This["username"] =value;} } [ConfigurationProperty ("Password", isrequired =true)] Public stringPassword {Get{return This["Password"]. ToString (); } Set{ This["Password"] =value;} } }
Accountconfiguration accountconfig = (accountconfiguration) configurationmanager.getsection (" Accountconfiguration"); Console.WriteLine (accountConfig.Users.UserName); Console.WriteLine (AccountConfig.Users.Password);
Third Kind
Configuration config = configurationmanager.openexeconfiguration (configurationuserlevel.none); = Config. GetSection ("system.net/mailsettings/smtp" as smtpsection; Console.WriteLine (section. from);
Fourth type
Http://www.cnblogs.com/liunlls/p/config.html
Fifth type
Configurationmanager.appsettings
Sixth type
Configurationmanager.connectionstrings
Of course there are many ...
C # Several ways to read a configuration file