C#读取配置文件的方法汇总
作者:hebedich 时间:2022-09-13 08:55:35
配置文件
<?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#读取配置文件
0
投稿
猜你喜欢
C#读写txt文件多种方法实例代码
2023-08-28 01:20:01
SpringCloud灾难性雪崩效应处理方法之降级实现流程详解
2022-03-24 15:31:36
详解Android中App的启动界面Splash的编写方法
2021-11-20 23:14:46
如何将默认的maven仓库改为阿里的maven仓库
2022-09-30 14:16:31
Android 仿网易新闻客户端分类排序功能
2023-12-19 13:07:52
Java中clone方法使用笔记
2023-03-26 07:44:38
java8实现List中对象属性的去重方法
2023-08-30 20:50:48
浅谈JAVA中输入输出流实例详解
2022-10-21 05:20:57
EL表达式的隐式对象_动力节点Java学院整理
2022-10-13 06:04:01
分享Java常用几种加密算法(四种)
2022-05-14 17:59:19
总结Android App内存优化之图片优化
2022-07-01 22:52:16
Spring Boot mybatis-config 和 log4j 输出sql 日志的方式
2021-05-31 22:13:20
C#中const,readonly和static关键字的用法介绍
2021-08-17 15:33:45
c# 组合模式
2022-07-07 09:04:09
C#单例模式(Singleton Pattern)实例教程
2022-11-23 10:44:05
java实现sftp客户端上传文件以及文件夹的功能代码
2023-02-14 22:07:28
Java自动添加重写的toString方法详解
2021-11-05 14:29:13
使用Logback设置property参数方式
2022-07-28 01:06:01
Android利用HorizontalScrollView仿ViewPager设计简单相册
2022-07-29 12:28:33
Java线程同步机制_动力节点Java学院整理
2023-08-01 10:29:47