What's the best practice to read values from appsettings in a windows forms .net 7 application? I have found examples for console application but it doesn't work.
Read and use appsettings parameters in a windows forms .net 6 application
1.6k views Asked by EICE At
2
There are 2 answers
0
On
Add the following NuGet packages to your project:
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json
appsettings.json
{
"MySetting": "Hello World!",
"MyOtherSetting": 123
}
Initialize appsettings.json
using Microsoft.Extensions.Configuration;
public partial class MyForm : Form
{
private readonly IConfiguration _configuration;
public MyForm()
{
InitializeComponent();
_configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
}
}
Get the details
string mySetting = _configuration.GetSection("MySetting").Value;
int myOtherSetting = _configuration.GetSection("MyOtherSetting").GetValue<int>();
Assuming this is for
appsettings.json, using the following fileUse the following classes to get values from
appsettings.jsonClass to read settings from
appsettings.jsonUsage
NuGet packages