.NET 使用配置文件

我简单测试了一下json格式配置文件,在这里记录一下,方便以后查看。

需要引用的库:

Microsoft.Extensions.Hosting

创建json格式文件:appsettings.json

在工程文件中包含json文件:

bash 复制代码
	<ItemGroup>
		<Content Include="appsettings.json">
			<CopyToOutputDirectory>Always</CopyToOutputDirectory>
		</Content>
	</ItemGroup>

在使用配置文件的位置可通过如下代码获取配置:

cs 复制代码
        var appSettings = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
        var tradeServerIP = appSettings.GetValue<string>("TradeServerIP");
        var tradeFrontPort = appSettings.GetValue<int>("TradeFrontPort");
相关推荐
公子小六17 小时前
基于.NET的Windows窗体编程之WinForms自定义控件(3)
windows·c#·.net·音视频·winforms
波力海苔夹心脆6752 天前
C# StringBuilder 详解:字符串拼接为什么总说要用它?
开发语言·笔记·c#·.net·字符串·stringbuilder·入门教程
1-1=02 天前
.NET Core 输出日志
c#·.net·.netcore
IT规划师2 天前
MQTT实战 - 生产环境MQTT丢包终极排查,8年经验总结(六)
物联网·.net
Elastic 中国社区官方博客3 天前
Observability:当 YAML 遇上 IConfiguration:OpenTelemetry .NET SDK 的声明式配置
大数据·开发语言·elasticsearch·搜索引擎·全文检索·.net
1-1=05 天前
.NET 实现后台任务
c#·.net·.netcore·quartz·hangfire
步步为营DotNet5 天前
从分代预算到 DATAS:.NET GC 调优到底在调什么
c#·.net·内存管理·gc·datas
学以智用5 天前
.NET 10 完整详解
.net
玩泥巴的5 天前
MAF + 飞书:让大模型干活,但别让它乱干
ai·c#·.net·飞书·agent