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 ...
随机推荐
- Git学习笔记(1)——安装,配置,创建库,文件添加到库
初次接触git,为了记忆深刻,把学习的简单流程记录下来. 本文记录了Git在Ubuntu上的安装,配置,以及创建版本库和往库中添加文件的过程. 1.Git的安装:(Ubuntu-Linux非常友好的安 ...
- CI Weekly #1 | 这份周刊,带你了解 CI/CD 、DevOps、自动化测试
原文首次发布与 flow.ci Blog >> 链接,转载请联系:) 准备了很久,CI Weekly 第一期终于来了. CI Weekly 围绕『 软件工程效率提升』 进行一系列技术内容分 ...
- eclipse导入cordova项目
eclipse导入cordova项目 导入老是出问题是不是?老是提议已存在是不是? 不知道如何改名字? 这里关键的一点是一定要选择对应的platform的目录,而不是你的项目的目录,不是你的项目的目录 ...
- 使用SSIS进行数据清洗
简介 OLTP系统的后端关系数据库用于存储不同种类的数据,理论上来讲,数据库中每一列的值都有其所代表的特定含义,数据也应该在存入数据库之前进行规范化处理,比如说"age"列 ...
- Command /usr/bin/codesign failed with exit code 1
刚刚碰到相同的问题,自己解决了,很简单,profile冲突,(自己遇到的现象是之前的profile关联的certificate过期,然后重新生成 了certificate和更新了profile.但是是 ...
- .NET面试题解析(05)-常量、字段、属性、特性与委托
系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 弱小和无知不是生存的障碍,傲慢才是!——<三体> 常见面试题目: 1. const和reado ...
- JSP网站开发基础总结《三》
经过前两篇的总结,我想大家一定迫不及待的想学习今天的关于jsp与mysql的数据库连接的知识了.既然需要连接mysql数据库,你首先需要保证你的电脑已经安装过mysql数据库,mysql数据库的安装步 ...
- NYOJ995硬币找零(简单dp)
/* 题意:给你不同面额的硬币(每种硬币无限多),需要找零的面值是T,用这些硬币进行找零, 如果T恰好能被找零,输出最少需要的硬币的数目!否则请输出剩下钱数最少的找零方案中的最少硬币数! 思路:转换成 ...
- gulp的使用
一.简介 gulp是一款前端构建工具,是和grunt很类似的一款构建工具,但是相比grunt来说,gulp更轻量级,配置和使用更简单,命令更少,更容易学习和记住. 二.具体的使用 安装gulp: np ...
- ZooKeeper官方文档翻译——ZooKeeper Overview 3.4.6
ZooKeeper ZooKeeper: A Distributed Coordination Service for Distributed Applications (针对分布式应用的分布式调度服 ...