How to work with configuration in c sharp when not using asp net core

with in C# ASP.NET Core

Configuration is an essential aspect of any software application, as it allows developers to define various settings and parameters that can be easily modified without changing the code. In C#, configuration can be handled using the ConfigurationManager class, provides a convenient way to access and manipulate configuration settings.

To work with configuration in C# without using ASP.NET Core, you can follow these steps:

Step 1: Add a Configuration File

The first step is to add a configuration file to your project. This file typically has the “.config” and contains key-value pairs that represent the configuration settings. For example, you can create a file named “app.config” and define settings like database connection strings, API keys, or any application-specific parameters.

Step 2: Access Configuration Settings

Once you have added the configuration file, you can access the configuration settings in your C# code using the ConfigurationManager class. This class provides a static method called “AppSettings” that returns a NameValueCollection containing all the key-value pairs defined in the configuration file.

using System.Configuration;

//  configuration settings
string databaseConnectionString = ConfigurationManager.AppSettings["DatabaseConnectionString"];
string apiKey = ConfigurationManager.AppSettings["ApiKey"];

In the above example, we retrieve the values of the “DatabaseConnectionString” and “ApiKey” settings from the configuration file.

Step 3: Modify Configuration Settings

If you need to modify the configuration settings at runtime, you can use the ConfigurationManager class to update the values. However, note that modifying the configuration file directly is not recommended, as it may require elevated privileges and can cause issues in certain scenarios.

Instead, you can use the ConfigurationManager class to update the configuration settings in memory. For example, to update the value of the “ApiKey” setting, you can do the following:

using System.Configuration;

//  configuration settings
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["ApiKey"].Value = "new-api-key";

In the above example, we open the configuration file for modification, update the value of the “ApiKey” setting, save the changes, and refresh the “appSettings” section to reflect the values.


Working with configuration in C# without using ASP.NET Core is straightforward using the ConfigurationManager class. By adding a configuration file, accessing the settings, and modifying them when necessary, you can easily manage the configuration of your C# applications.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents