• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#读取配置文件的方法汇总

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

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了马桶c的个人空间,c语言,欲情 c max,维生素c,crh2c等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

配置文件

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

</div>

第一种

  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; }
    }
  }

</div>
      SQLConfiguration sqlConfig = (SQLConfiguration)ConfigurationManager.GetSection("SQLConfiguration");
      Console.WriteLine(sqlConfig.Type);
      Console.WriteLine(sqlConfig.ConnectionString);
</div>

第二种

  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; }
    }
  }

</div>
     AccountConfiguration accountConfig = (AccountConfiguration)ConfigurationManager.GetSection("AccountConfiguration");
      Console.WriteLine(accountConfig.Users.UserName);
      Console.WriteLine(accountConfig.Users.Password);
</div>

第三种

      Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      SmtpSection section = config.GetSection("system.net/mailSettings/smtp") as SmtpSection;
      Console.WriteLine(section.From);
</div>

第四种

http://www.weikejianghu.com/article/53615.htm

第五种

 ConfigurationManager.AppSettings

第六种

 ConfigurationManager.ConnectionStrings

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

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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#利用ReportViewer生成报表
  • C#基于正则去掉注释的方法示例
  • C#中new的用法及与override的区别分析
  • C#实现两个richtextbox控件滚动条同步滚动的简单方法
  • C# for循环的经典案例集锦
  • C#操作word的方法示例
  • C#使用WebClient登录网站并抓取登录后的网页信息实现方法
  • C# WinForm制作异形窗体与控件的方法
  • C#实现Excel表数据导入Sql Server数据库中的方法
  • C#使用NPOI上传excel

相关文章

  • 2017-05-28C#操作XML通用方法汇总
  • 2017-05-28浅谈C# 序列化与反序列化几种格式的转换
  • 2017-05-28C#中将DataTable转换成CSV文件的方法
  • 2017-05-28c#判断字符是否为中文的三种方法分享(正则表达式判断)
  • 2017-05-28C# 解析 Excel 并且生成 Csv 文件代码分析
  • 2017-05-28C#中WebClient实现文件下载
  • 2017-05-28C# 多线程读取注册表,加载至TreeView
  • 2017-05-28C#实现Excel动态生成PivotTable
  • 2017-05-28asp.net中XML如何做增删改查操作
  • 2017-05-28C# Console类的具体用法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • c#构造初始化的顺序浅析
    • C#中List和数组之间转换的方法
    • C#使用迭代法实现Fibnaci数列
    • 在C#中创建和读取XML文件的实现方法
    • C#中单例模式的三种写法示例
    • C# 判断字符串为空的几种办法
    • 分享用于操作FTP的客户端C#类
    • C#从控制台读取字符串的方法
    • C#常见的几种集合 ArrayList,Hashtable,List<T>,Dictionary<K,V> 遍历方法对比
    • C# 改变无边框窗体尺寸大小的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有