C#读取配置文件的方法汇总

 更新时间:2015年06月17日 08:52:11   投稿:hebedich  
本文给大家介绍的是使用C#读取配置文件的方法,个人给大家总结了6种,余下的以后再更新,有需要的小伙伴可以参考下。

配置文件

<?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);

第四种

https://www.jb51.net/article/53615.htm

第五种

 ConfigurationManager.AppSettings

第六种

 ConfigurationManager.ConnectionStrings

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

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

  • C# 游戏外挂实现核心代码

    C# 游戏外挂实现核心代码

    最近打算学习下游戏外挂,因为c#语言,感觉比较顺,高手用delphi的多,不知道哪个最好。
    2009-01-01
  • C#使用log4net的3种调用方法

    C#使用log4net的3种调用方法

    log4net是一个用于记录日志的开源框架,它是C#中最常用的日志记录工具之一,本文给大家介绍了C#使用log4net的3种调用方法,通过图文和代码给大家讲解的非常详细,需要的朋友可以参考下
    2024-03-03
  • C#实现3步手动建DataGridView的方法

    C#实现3步手动建DataGridView的方法

    这篇文章主要介绍了C#实现3步手动建DataGridView的方法,实例分析了C#实现手动创建DataGridView的原理与技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • 在C#中使用二叉树实时计算海量用户积分排名的实现详解

    在C#中使用二叉树实时计算海量用户积分排名的实现详解

    这篇文章主要介绍了在C#中使用二叉树实时计算海量用户积分排名的实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • C#读取Excel的三种方式以及比较分析

    C#读取Excel的三种方式以及比较分析

    这篇文章主要介绍了C#读取Excel的三种方式以及比较分析,需要的朋友可以参考下
    2015-11-11
  • 解析C#设计模式编程中适配器模式的实现

    解析C#设计模式编程中适配器模式的实现

    这篇文章主要介绍了C#设计模式编程中适配器模式的实现,分别举了类的对象适配器与对象的适配器模式的例子,需要的朋友可以参考下
    2016-02-02
  • C#如何判断.Net Framework版本是否满足软件运行需要的版本

    C#如何判断.Net Framework版本是否满足软件运行需要的版本

    这篇文章主要介绍了C#如何判断.Net Framework版本是否满足软件运行需要的版本问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • C# 列表List的常用属性和方法介绍

    C# 列表List的常用属性和方法介绍

    这篇文章主要介绍了C# 列表List的常用属性和方法介绍,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • C# SortedList排序列表的实现

    C# SortedList排序列表的实现

    本文主要介绍了C# SortedList排序列表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • C# 抽象类,抽象属性,抽象方法(实例讲解)

    C# 抽象类,抽象属性,抽象方法(实例讲解)

    下面小编就为大家分享一篇C# 抽象类,抽象属性,抽象方法的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12

最新评论