C#读取配置文件的几种方式

2018-08-02 06:26:44来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

配置文件

<?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.cnblogs.com/liunlls/p/config.html

第五种

 ConfigurationManager.AppSettings

第六种

 ConfigurationManager.ConnectionStrings

当然还有很多......

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:前端——HTML,CSS

下一篇:命名规范(1)大小写约定