C#讀取配置文件的方法匯總
配置文件
<?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="liunian@qq.com">
<network />
</smtp>
</mailSettings>
</system.net>
</configuration>
第一種
class SQLConfiguration : ConfigurationSection
{
[ConfigurationProperty("type", IsRequired = true)]
public string Type
{
get { return this["type"].ToString(); }
set { this["type"] = value; }
}
[ConfigurationProperty("connectionString", IsRequired = true)]
public string ConnectionString
{
get { return this["connectionString"].ToString(); }
set { this["connectionString"] = value; }
}
}
SQLConfiguration sqlConfig = (SQLConfiguration)ConfigurationManager.GetSection("SQLConfiguration");
Console.WriteLine(sqlConfig.Type);
Console.WriteLine(sqlConfig.ConnectionString);
第二種
public class AccountConfiguration : ConfigurationSection
{
[ConfigurationProperty("users", IsRequired = true)]
public AccountSectionElement Users
{
get { return (AccountSectionElement)this["users"]; }
}
}
public class AccountSectionElement : ConfigurationElement
{
[ConfigurationProperty("username", IsRequired = true)]
public string UserName
{
get { return this["username"].ToString(); }
set { this["username"] = value; }
}
[ConfigurationProperty("password", IsRequired = true)]
public string Password
{
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);
第三種
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
SmtpSection section = config.GetSection("system.net/mailSettings/smtp") as SmtpSection;
Console.WriteLine(section.From);
第四種
http://www.fzitv.net/article/53615.htm
第五種
ConfigurationManager.AppSettings
第六種
ConfigurationManager.ConnectionStrings
當(dāng)然還有很多......
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
c#刪除數(shù)組中符合條件的元素(正確寫(xiě)法)
這篇文章主要介紹了c#刪除數(shù)組中符合條件的元素,分別給大家展示了錯(cuò)誤寫(xiě)法和正確寫(xiě)法,補(bǔ)充介紹了從C#的數(shù)組中刪除指定元素的幾種方法,需要的朋友可以參考下2023-10-10
Unity3D動(dòng)態(tài)對(duì)象優(yōu)化代碼分享
這篇文章主要介紹了Unity3D動(dòng)態(tài)對(duì)象優(yōu)化代碼分享的相關(guān)資料,需要的朋友可以參考下2015-03-03
C#使用Socket快速判斷數(shù)據(jù)庫(kù)連接是否正常的方法
這篇文章主要介紹了C#使用Socket快速判斷數(shù)據(jù)庫(kù)連接是否正常的方法,涉及C#中socket操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
.NET中的async和await關(guān)鍵字使用及Task異步調(diào)用實(shí)例
這篇文章主要介紹了.NET中的async和await關(guān)鍵字使用及Task異步調(diào)用實(shí)例,本文還包含了取消執(zhí)行和顯示進(jìn)度的例子,需要的朋友可以參考下2014-07-07
C#開(kāi)源的AOP框架--KingAOP基礎(chǔ)
這篇文章主要介紹了一款C#開(kāi)源的AOP框架--KingAOP框架的基礎(chǔ)知識(shí),對(duì)于想學(xué)習(xí)AOP的小伙伴來(lái)說(shuō),非常不錯(cuò),希望大家能夠喜歡。2015-12-12
Visual Studio連接unity編輯器的實(shí)現(xiàn)步驟
unity編輯器中打開(kāi)C#腳本的時(shí)候發(fā)現(xiàn)Visual Studio沒(méi)有連接unity編輯器,本文主要介紹了Visual Studio連接unity編輯器的實(shí)現(xiàn)步驟,感興趣的可以了解一下2023-11-11
C# SendMail發(fā)送郵件功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了C# SendMail發(fā)送郵件功能實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

