.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");
相关推荐
慧都小妮子2 小时前
Aspose.CAD for .NET 26.6 更新发布
pdf·.net·3d渲染·cad·pdf导出·ifc编辑
夜莺悠吟8 小时前
关于对 C# 中 ImplicitUsings,GlobalUsings 的讨论
c#·.net
fen_fen1 天前
麒麟 Linux aarch64安装.NET 6,并适配.net应用(达梦数据库)
linux·服务器·.net
黑洞视界3 天前
高性能进程内队列 BufferQueue 1.0 版本发布
.net
fogota3 天前
【AI】C# .NET8 WPF 第三方DLL引用配置(编译不丢失、不自动删除)
c#·.net·wpf·dll
飞塔老梅子4 天前
【FortiOS 8.0】❀ 07. SSID桥接模式 ❀ FortiGate 防火墙
配置·桥接·fortios·ssid·8.0