.NET CORE CONSOLE TO GET VALUES FROM APPSETTING.JSON

Open your Visual Studio then select Create a new project.

Select Console App.

Name our new project GetAppSettingJsonValues.

For the Framework, i choose .NET 8, because that is the only .NET version i do have on my computer. You might want to choose other version.

Click Create button. A window with two panels will be opened side by side. The right panel is Solution Explorer as you can see below. By default the .NET CORE console app will generate Program.cs file for you.

The left panel of the window is for editing Program.cs. If you are on lower version of .NET CORE, you may still get the Main() static method as a pregenerated code. Because i am on .NET CORE 8, the format has changed or simplified like seen below.

Go back to Solution Explorer panel again, right click your mouse on the project name and select Add, New Item, Text File, change the name of the file to appsetting.json followed by clicking Add button.

On the left panel of your window, your new appsetting.json is opened and copy the following json data into that file.

{
"smtp": {
"host": "192.168.57.31",
"port": 25,
"username": "johndoe",
"password": "nutrient"
}
}

Select your project name on Solution Explorer panel. Right click, select Manage Nuget Package. A nuget package manager window will appear on your left panel. Click the Browse tab. and type Microsoft.Extensions.Configuration on the search text box. You would see Microsoft.Extension.Configuration package on the search result. Select that package and install it to your project.

You need to click Apply button to confirm your package installation.

The next package you need is Microsoft.Extensions.Configuration.Binder. You need to repeat the process. Type that package name on search text box, select it from the search result list and then install it.

The third package you need is Microsoft.Extensions.Configuration.Json.

Copy the following C# code below to Program.cs file on the left panel of the window.

using Microsoft.Extensions.Configuration;

namespace MyApp
{
internal class Program
{
static void Main(string[] args)
{
IConfiguration builder = new ConfigurationBuilder().AddJsonFile("appsetting.json").Build();
ConfigurationManager manager = new ConfigurationManager();
manager.AddConfiguration(builder);
string? host = manager.GetSection("smtp").GetValue<string>("host");
int? port = manager.GetSection("smtp").GetValue<int>("port");
string? username = manager.GetSection("smtp").GetValue<string>("username");
string? password = manager.GetSection("smtp").GetValue<string>("password");
Console.WriteLine("Host => {0}", host);
Console.WriteLine("Port => {0}", port);
Console.WriteLine("Username => {0}", username);
Console.WriteLine("Password => {0}", password);
}
}
}

On the Solution Explorer, select the project name, right click and select Build. You will see a bin folder being built. This folder has sub folder which named by default based on your .NET CORE version. Because i am on version 8, the sub folder i am getting net8.0.

Before we can run our console application, we need to copy our appsetting.json from project root folder to that sub folder. Once you have done it, press F5 to run the application.

You can download the project code from this link.

BARCHART USING SCOTTPLOT

Run your Visual Studio IDE, select Console App follow by clicking NEXT button.

Name our new project ScottPlotBarChart. Click NEXT.

Select any framework that suit you, in may case is .NET 8. Click NEXT.

On Solution Explorer, right click and select Manage Nuget Package.

On the pop up window, select Browser tab and type ScottPlot on text box. Soon after that select ScottPlot nuget package from the list below.

Now cut and paste the following code below and click F5 to run.

using ScottPlot;
using ScottPlot.Legends;

namespace ScottPlotBarChart
{
class Program
{
static void Main(string[] args)
{
Bar bar1 = new Bar();
bar1.Value = 75000;
bar1.Position = 1;
bar1.Size = 0.5;
bar1.FillColor = ScottPlot.Color.FromHex("AC92EB");

Bar bar2 = new Bar();
bar2.Value = 58000;
bar2.Position = 2;
bar2.FillColor = ScottPlot.Color.FromHex("4FC1E8");
bar2.Size = 0.5;

Bar bar3 = new Bar();
bar3.Value = 45000;
bar3.Position = 3;
bar3.FillColor = ScottPlot.Color.FromHex("A0D568");
bar3.Size = 0.5;

Bar bar4 = new Bar();
bar4.Value = 91500;
bar4.Position = 4;
bar4.FillColor = ScottPlot.Color.FromHex("FFCE54");
bar4.Size = 0.5;

Bar bar5 = new Bar();
bar5.Value = 67500;
bar5.Position = 5;
bar5.FillColor = ScottPlot.Color.FromHex("ED5564");
bar5.Size = 0.5;

Tick[] ticks =
{
new(1, "John"),
new(2, "Marry"),
new(3, "Martha"),
new(4, "Isac"),
new(5, "Harleen"),
};

Plot myPlot = new();
myPlot.HideGrid();
myPlot.Title("Employee Salaries", 14);
myPlot.XLabel("Employees");
myPlot.YLabel("Salaries");
myPlot.Add.Bar(bar1);
myPlot.Add.Bar(bar2);
myPlot.Add.Bar(bar3);
myPlot.Add.Bar(bar4);
myPlot.Add.Bar(bar5);

myPlot.Axes.Bottom.TickGenerator = new ScottPlot.TickGenerators.NumericManual(ticks);
myPlot.Axes.Bottom.MajorTickStyle.Length = 0;
myPlot.Axes.Margins(bottom: 0);
myPlot.SavePng(@"c:\ScottPlotBarChart.png", 800, 600);
myPlot.Dispose();
}
}
}

After you ran the code above you would see a png file been generated.

If you want the source code, please click on this link