ConfigurationManager.AppSettings in returning null
One, perhaps easier, alternative is to use a Settings file. This encapsulates the creation and maintenance of App.config values in a designer GUI and generates code for accessing the values.
To add a Settings file, right click your project in VS and click 'Add -> New Item', select 'Settings file' and give it a meaningful name, e.g. MainSettings.settings
. You can then add an item, e.g. Foo
, specify whether it is application or user-wide, define it's type and a assign it a value. In your code you can retreive the value by simple writing MainSettings.Default.Foo
.
After compilation, you can change the value by editing the config file. The setting will appear as follows:-
<applicationSettings>
<YourNamespace.MainSettings>
<setting name="Foo" serializeAs="String">
<value>Bar</value>
</setting>
</YourNamespace.MainSettings>
</applicationSettings>
ConfigurationManager return null instead of string values
Did you ensure that the config file is placed correctly at the directory from which you're running the application? Is there actually a file called <app name>.exe.config in that directory?
I'm just guessing here - maybe you added the App.Config file in a different project then your exe assembly project...?
By the way, I copied your code and App.Config as is to a clean project, and this code worked for me. So I'd look in the direction of the config file itself and not in the code. The code is fine...
Hope this helps,
Ran
URL setting in configuration file returns null
You didn't follow your tutorial properly, if you read "Steps to read AppSettings from External Config File using ConfigurationManager" section carefully, you will see:
Steps to read AppSettings from External Config File using ConfigurationManager:
App.config File
<configuration>
<appSettings configSource="Configurations\Environment.config" />
</configuration>
Create another config file and name it Environment.config under the same project.
Environment.config File
<appSettings>
<add key="URL" value="http://www.example.com"/>
</appSettings>
To read the connection string from the above config file, use the below code:
var url = ConfigurationManager.AppSettings["URL"];
You need to add an external SomeConfigFile.config
file path/reference to your App.config
file before using ConfigurationManager.AppSetting["key"]
.
ConfigurationManager.AppSettings - Returns Null
Ive tried moving the app-config to different projects in the solution and ive recreated the file, but with no luck
your code is correct (i have tested), you need to make sure that your app.config file is in your main project (exe).
Related Topics
Change Datagrid Cell Colour Based on Values
C# - How to Save Byte Values to File With Smallest Size Possible
How to Connect to a Database in ASP.NET Core Without Entity Framework
Add Client Certificate to .Net Core Httpclient
Reading from Excel File Using Closedxml
How to Get the Specific Column in Excel Worksheet Using Documentformat.Openxml C#
How to Autosize the Height and the Width of C# Windows Form
Web-Api Post Body Object Always Null
How to Generate Database Tables from C# Classes
Getting Value of Selected Item in List Box as String
How to Ignore the Certificate Check When Ssl
Copy Few Items from One List to Another List of Same Type in C#
C# Double - Tostring() Formatting With Two Decimal Places But No Rounding
How to Calculate the Average of Each Row in Multidimensional Array