Reading settings from app.config or web.config in .NET
You'll need to add a reference to System.Configuration
in your project's references folder.
You should definitely be using the ConfigurationManager
over the obsolete ConfigurationSettings
.
Reading from a app.config file
below code gives you the content of active config file.
var content = File.ReadAllLines(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
Check what you get as content
, is it contain key="name" value="Chan"
or something else
?
if you given <add key="name" value="Chan" />
then
ConfigurationManager.AppSettings["name"] should return as Chan
AppSettings get value from .config file
This works for me:
string value = System.Configuration.ConfigurationManager.AppSettings[key];
Reading a string from app.config file
You are storing it in the wrong place in the app.config. For single name/value pairs use the appSettings
. Then your retrieval code in the question will work.
<configuration>
<appSettings>
<add key="Location_Name" value="String I want" />
</appSettings>
</configuration>
Code (repeated from question)
var value = ConfigurationManager.AppSettings["Location_Name"];
Can't read keys from App.config
I found the solution! The problem was, that I debugged a unit test, which was located in a different sub-project within the same solution. I copied the app.config file to the same project as the unit test and it works fine now. thanks for your help!
Read configuration from App.config instead of AppName.exe.config
App.config and AppName.exe.config aren't exactly separate - AppName.exe.config is the build output of app.config.
In other words, once the service is deployed, there shouldn't even be an app.config to change, and if there is, changing it won't do anything. AppName.exe.config is where your application gets "app.config" values.
If you need different configuration values for different environments, take a look at config transformations. They allow you to change or replace sections or individual values for different configurations such as debug, release, or other custom configurations.
This is useful because it means that all of the connection strings are part of the project and are in source control. You can do without this and just edit the config file in place on the server, but then there will be nothing in source control indicating where the connection string came from. And then if you redeploy the service the change will be overwritten unless someone remembers to make the same change every time. Having it in source control is much better.
For some reason that I do not understand, you can right-click a web.config and select "Add Config Transforms", but you can't do that with an app.config. Maybe there's a good reason.
You can install this extension which enables the same behavior for app.config. Then you can right-click on app.config and add a transformation for another configuration, like Release.
In that file (app.Release.config) add this:
<connectionStrings xdt:Transform="Replace">
<add name="testString" connectionString="...your Release connection string..." />
</connectionStrings>
When you build and deploy using the Release configuration it will replace the connectionStrings
section with this one, leaving everything else just as it is. You can also right-click app.Release.config and select "Preview Config Transforms" to see the transformed file side-by-side with the original.
Read from App.config in a Class Library project
As stated in my comment, add the App.Config file to the main solution and not in the class library project.
Related Topics
Micro Optimization of a 4-Bucket Histogram of a Large Array or List
How to Add Code Outside the Scope of Main When Using C# 9 Top Level Statements
Asmx Web Service How to Return JSON and Not Xml
Digital Signature in C# Without Using Bouncycastle
Converting Integers to Roman Numerals
Multiple Where Clauses with Linq Extension Methods
JSON.Net (De)Serialize Untyped Property
What Are the Differences Between the Xmlserializer and Binaryformatter
Returning a String from a C# Dll with Unmanaged Exports to Inno Setup Script
Use Local Images in Webbrowser Control
In C#, Is "This" Keyword Required
Difference Between Icomparable and Icomparer
Best Way to Share Data Between Two Child Components in Blazor
Binding to Custom Control Inside Datatemplate for Itemscontrol
The Difference Between Virtual, Override, New and Sealed Override