1、项⽬增加应⽤程序配置⽂件fig
2、⽂件设置为“如果较新则复制”
3、⽰例⽂件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>剑灵没活力了怎么办
立冬是几月几日2018年<appSettings>
<add key="DBConString" value="数据库连接字符串"/>
</appSettings>
读取配置文件失败</configuration>
6大症状是更年期前兆4、读取通⽤⽅法
public static string GetSettings(string key)
{
return System.Configuration.ConfigurationManager.AppSettings[key];
}
⼆、使⽤appsettings.json作为配置⽂件
1、在项⽬中添加appsettings.json⽂件,设置“复制到输出⽬录”为“如果较新则复制”
2、读取通⽤⽅法
static string GetSettings(string key)
{
var builder = new ConfigurationBuilder()
朱时茂与美女吻别.SetBasePath(Directory.GetCurrentDirectory())
.
AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
return configuration[key];
}
3、⽰例节点
{
"RabbitMQ": {
"host": "主机地址",
"user": "⽤户",
"password": "密码"
}
}
4、读取⽰例代码
var factory = new ConnectionFactory()
{ HostName = GetSettings("RabbitMQ:host"),
UserName = GetSettings("RabbitMQ:user"), Password = GetSettings("RabbitMQ:password") };
>夜扣宫门
发布评论