.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");
相关推荐
geovindu1 天前
CSharp: Flyweight Pattern
开发语言·后端·c#·.net·享元模式·结构型模式
驰骋工作流1 天前
请假流程:六款.NET工作流引擎实现方式对比
.net·ccflow·工作流引擎二开·.net工作流引擎对比
WarPigs1 天前
.NET项目app.Config笔记
c#·.net
海盗12342 天前
微软技术周报——2026-07-22
microsoft·c#·.net
海盗12342 天前
微软技术日报 ——2026-07-21
microsoft·c#·.net
半亩码田2 天前
【.NET新特性·第8篇】.NET 9 AI 构建基块:Microsoft.Extensions.AI
人工智能·microsoft·.net
Ctrl+Z侠3 天前
.NET WebApi Windows / Linux Docker 全链路压测与瓶颈定位 0 到 1 教程
linux·windows·.net
慧都小妮子3 天前
Aspose.CAD for .NET 26.6 更新发布
pdf·.net·3d渲染·cad·pdf导出·ifc编辑
夜莺悠吟3 天前
关于对 C# 中 ImplicitUsings,GlobalUsings 的讨论
c#·.net