'How to use ConfigurationManager
I want to use App.config for storing some setting. I tried to use the next code for getting a parameter from a config file.
private string GetSettingValue(string paramName)
{
return String.Format(ConfigurationManager.AppSettings[paramName]);
}
I also added System.Configuration for it (I used a separate class), and in App.config file I have:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key ="key1" value ="Sample" />
</appSettings>
</configuration>
But I got an error while trying to use ConfigurationManager - ConfigurationManager can't exist in such context, but I already added System.Configuration. Or did I miss something?
EDIT:
class with config (full view)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace browser
{
class ConfigFile
{
private string GetSettingValue(string paramName)
{
return String.Format(ConfigurationManager.AppSettings[paramName]);
}
}
}
EDIT2
Add how it looks

This means the problem is not during using ConfigurationManger but before - the program "says" that it "doesn't know such element" as I understand the error - the "Element ConfigurationManager" doesn't exist in such context"
EDIT3

EDIT 4

Solution 1:[1]
Okay, it took me a while to see this, but there's no way this compiles:
return String.(ConfigurationManager.AppSettings[paramName]);
You're not even calling a method on the String type. Just do this:
return ConfigurationManager.AppSettings[paramName];
The AppSettings KeyValuePair already returns a string. If the name doesn't exist, it will return null.
Based on your edit you have not yet added a Reference to the System.Configuration assembly for the project you're working in.
Solution 2:[2]
Go to tools >> nuget >> console and type:
Install-Package System.Configuration.ConfigurationManager
If you want a specific version:
Install-Package System.Configuration.ConfigurationManager -Version 4.5.0
Your ConfigurationManager dll will now be imported and the code will begin to work.
Solution 3:[3]
Add a reference to System.Configuration.dll by following these steps:
On the Project menu, select Add Reference.
In the Add Reference dialog box, select the .NET tab.
Find and select the Component Name of System.Configuration.
select OK.
This should add the ConfigurationManager
Store and retrieve custom information from an application configuration file
Solution 4:[4]
I found some answers, but I don't know if it is the right way. This is my solution for now. Fortunately it didn't break my design mode.
/// <summary>
/// set config, if key is not in file, create
/// </summary>
/// <param name="key">Nome do parĂ¢metro</param>
/// <param name="value">Valor do parĂ¢metro</param>
public static void SetConfig(string key, string value)
{
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;
if (settings[key] == null)
{
settings.Add(key, value);
}
else
{
settings[key].Value = value;
}
configFile.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
}
/// <summary>
/// Get key value, if not found, return null
/// </summary>
/// <param name="key"></param>
/// <returns>null if key is not found, else string with value</returns>
public static string GetConfig(string key)
{
return ConfigurationManager.AppSettings[key];
}
Solution 5:[5]
Using ASP .NET6
nstall NuGet package System.Configuration.ConfigurationManager
The name of my configuration file is App.Config
Content of App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="DBConnectionString" value="aa" />
<add key="DBName" value="bb" />
<add key="AuthKey" value="cc" />
</appSettings>
</configuration>
File reading values, i.e. SettingsService.cs:
using namespc = System.Configuration;
namespace Ecommerce_server.Services.SetupOperation
{
public interface ISettingsService
{
string MongoDBConnectionString { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }
string DataBaseName { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }
public string AuthKey { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }
}
public class SettingsService : ISettingsService
{
public string MongoDBConnectionString { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }
public string DataBaseName { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }
public string AuthKey { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }
}
}
Note: We had to create a specialized namespace for ConfigurationManger to solve ambiguity.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | bausa |
| Solution 2 | Giulio Caccin |
| Solution 3 | isubodh |
| Solution 4 | CJ Dennis |
| Solution 5 | Franco |
