程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#讀取設置裝備擺設文件的辦法匯總

C#讀取設置裝備擺設文件的辦法匯總

編輯:C#入門知識

C#讀取設置裝備擺設文件的辦法匯總。本站提示廣大學習愛好者:(C#讀取設置裝備擺設文件的辦法匯總)文章只能為提供參考,不一定能成為您想要的結果。以下是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="[email protected]">
    <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.jb51.net/article/53615.htm

第五種

 ConfigurationManager.AppSettings

第六種

 ConfigurationManager.ConnectionStrings

固然還有許多......

以上所述就是本文的全體內容了,願望年夜家可以或許愛好。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved