.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");
相关推荐
123的故事9 小时前
工具分享(2)-NSmartProxy内网穿透工具。
c#·.net·nsmartproxy
SEO-狼术10 小时前
Bring Agentic AI to RAD Studio
.net
足球中国11 小时前
.net winform 仿excel控件 开源地址 https://github.com/yufb12/dataexcel_winform
.net
周杰伦fans11 小时前
深入浅出AutoCAD .NET二次开发:HostApplicationServices完全解析
数据库·.net
caimouse11 小时前
Godot 4.7 内嵌 C# 模块切换到 .NET 9.0 编译指南
c#·.net·godot
rockey62721 小时前
基于AScript的SQL脚本语言发布啦!
sql·c#·.net·script·expression·动态脚本
喵叔哟1 天前
14【.NET10 实战--孢子记账--产品智能化】--智能生成预算
大数据·人工智能·.net
mincau1 天前
.NET 反编译代码语义化还原:三工具协同实战
.net·反编译·ai辅助
调试优选官2 天前
2026GEO优化工具软件技术路径拆解:从监测机制到工程落地
.net·软件开发·技术分享·geo
毛驴赶鹿2 天前
Serv-U 配合 cpolar 实现外网 SFTP 访问,一次配置永久生效
sftp·配置