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=""></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
当然还有很多......
C#读取配置文件的几种方式的更多相关文章
- Spring Boot 入门系列(二十五)读取配置文件的几种方式详解!
在项目开发中经常会用到配置文件,之前介绍过Spring Boot 资源文件属性配置的方法,但是很多朋友反馈说介绍的不够详细全面.所以, 今天完整的分享Spring Boot读取配置文件的几种方式! S ...
- Spring Boot读取配置文件的几种方式
Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口.这三种注解可以配合着@PropertySou ...
- spring-boot-route(二)读取配置文件的几种方式
Spring Boot提供了两种格式的配置文件,分别是properties 和 yml.Spring Boot最大的特点就是自动化配置,如果我们想修改自动化配置的默认值,就可以通过配置文件来指定自己服 ...
- java 学习笔记 读取配置文件的三种方式
package com.itheima.servlet.cfg; import java.io.FileInputStream; import java.io.FileNotFoundExceptio ...
- 关于spring读取配置文件的两种方式
很多时候我们把需要随时调整的参数需要放在配置文件中单独进行读取,这就是软编码,相对于硬编码,软编码可以避免频繁修改类文件,频繁编译,必要时只需要用文本编辑器打开配置文件更改参数就行.但没有使用框架之前 ...
- Servlet读取配置文件的三种方式
一.利用ServletContext.getRealPath()[或getResourceAsStream()] 特点:读取应用中的任何文件.只能在web环境下. private void text3 ...
- Spring 读取配置文件的俩种方式
读取配置可通过 org.springframework.core.env.Environment 类来获取, 也可以通过@Value的方式来获取 注解形式: @PropertySource({&quo ...
- Spring读取配置文件的几种方式
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; imp ...
- SpringBoot中读取配置文件的几种方式
1.读取application文件 在application.yml或者properties文件中添加: info: name: xiaoming age: 13 sex: 1 读取方式如下: imp ...
随机推荐
- 免费打造自己的个人网站,免费域名、免费空间、FTP、数据库什么的,一个不能少,没钱,也可以这么任性
作为一名程序猿,拥有自己的个人网站,是一件多么有逼格的事~~至于个人网站的好处嘛?那是多的说都说不完啊~~例如我们可以放自己的作品,展示自己的风采,放自己女神的照片(女神看到后会怎么样,自己想吧,哈哈 ...
- java中的Static class
Java中的类可以是static吗?答案是可以.在java中我们可以有静态实例变量.静态方法.静态块.类也可以是静态的. java允许我们在一个类里面定义静态类.比如内部类(nested class) ...
- Android多线程分析之二:Thread的实现
Android多线程分析之二:Thread的实现 罗朝辉 (http://www.cnblogs.com/kesalin/) CC 许可,转载请注明出处 在前文<Android多线程分析之一 ...
- How to Change RabbitMQ Queue Parameters in Production?
RabbitMQ does not allow re-declaring a queue with different values of parameters such as durability, ...
- CocoaPods 使用
为什么要使用这个玩意呢,最近在使用swift开发项目,使用 swift 开源库的时候,在git上下载后居然不知道哪些是必须文件,还要思考下,看看哪些是需要的(不像原来oc开源库,一目了然),网上使用d ...
- 说说设计模式~装饰器模式(Decorator)~多功能消息组件的实现
返回目录 为何要设计多功能消息组件 之前写过一篇装饰器模式的文章,感觉不够深入,这次的例子是实现项目中遇到的,所以把它拿出来,再写写,之前也写过消息组件的文章,主要采用了策略模式实现的,即每个项目可以 ...
- 知方可补不足~sqlserver中触发器的使用
回到目录 触发器在过去的10年中,即存储过程和ado.net称霸江湖期间是那么的重要,而现在,trigger显得不是那么必要的,我们很少将复杂的业务写在SQL里,当然也会没有机会写到trigger里了 ...
- 使用Sublime Text 2 编辑Markdown
http://www.ituring.com.cn/article/6815 一.安装 下载Sublime Text 2 安装 二.安装Package Control 按Ctrl + ` 打开cons ...
- iOS开发——高级技术&广告服务
广告服务 上 面也提到做iOS开发另一收益来源就是广告,在iOS上有很多广告服务可以集成,使用比较多的就是苹果的iAd.谷歌的Admob,下面简单演示一下如何 使用iAd来集成广告.使用iAd集成广告 ...
- python入门学习课程推荐
最近在学习自动化,学习过程中,越来越发现coding能力的重要性,不会coding,基本不能开展自动化测试(自动化工具只是辅助). 故:痛定思痛,先花2个星期将python基础知识学习后,再进入自动化 ...