DEV Community

Cover image for How to C#: add a settings file to your Console application
Roel
Roel

Posted on • Edited on

29 2

How to C#: add a settings file to your Console application

What if I want to create a quick console application and keep all my settings in a seperate appsettings.json file? The default C# application template doesn't provide this for us, so I think it might be useful to write a little guide to make this easier in the future.

Required

  • .Net 5.0 console application
  • The following packages:
<ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
</ItemGroup>
Enter fullscreen mode Exit fullscreen mode

Guide

Simply follow these steps to load your appsettings into a class for easy use.

  • Add the references above to the project
  • Add a new class called ApiSettings.cs
    public class ApiSettings
    {
        public string TwitterApiKey { get; set; }
        public string TwitterApiSecret { get; set; }
        public string BearerToken { get; set; }
    }
Enter fullscreen mode Exit fullscreen mode
  • Add a new class called Startup.cs
public class Startup
    {
        public Startup()
        {
            var builder = new ConfigurationBuilder()
                      .SetBasePath(Directory.GetCurrentDirectory())
                      .AddJsonFile("appsettings.json", optional: false);

            IConfiguration config = builder.Build();

            ApiSettings = config.GetSection("ApiSettings").Get<ApiSettings>();

        }

        public ApiSettings ApiSettings { get; private set; }
    }
Enter fullscreen mode Exit fullscreen mode
  • Add your appsettings.json with content similar to something like this:
  {
"ApiSettings": {
    "TwitterApiKey": "",
    "TwitterApiSecret": "",
    "BearerToken": ""
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Right click on the file appsettings.json and set the copy output to Copy always

Image description

  • Make sure the fields in the section 'ApiSettings' match your ApiSettings.cs class.

  • Go back to your Program.cs file and add the following line:
    var startup = new Startup();

  • We can now use the values in the ApiSettings section of our appsettings. You can try it out with the following code.

Image description

All Done!

Now you can experiment and make it even more advanced yourself!

For a more advanced tutorial implementing this into an API look here https://dev.to/iamrule/how-to-c-add-configuration-to-your-net-8-web-api-application-4cjf

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (3)

Collapse
 
vaso profile image
Vaclav Elias

Your required section should be saying .NET 6 instead of .NET 5 as you are referencing .NET 6 NuGet packages.

Collapse
 
iamrule profile image
Roel

Hey, thanks for your comment! The v6.0 packages actually work on a .net5 application in my example. I will update the article to reference the v5 packages to make it easier to understand for new readers.

Collapse
 
vaso profile image
Vaclav Elias

You are welcome. I know that it works, I only mentioned it so we don't confuse new learners 🤣

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay