I'm not able to access values in configuration file.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var clientsFilePath = config.AppSettings.Settings["ClientsFilePath"].Value;
// the second line gets a NullReferenceException
.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- ... -->
<add key="ClientsFilePath" value="filepath"/>
<!-- ... -->
</appSettings>
</configuration>
Do you have any suggestion what should I do?
In the app/web .config file set the following configuration:
then you can access this in your code by putting in this line:
*Note that this work fine for .net4.5.x and .net4.6.x; but do not work for .net core. Best regards: Rafael
For web application, i normally will write this method and just call it with the key.
See I did what I thought was the obvious thing was:
While that compiles it always returns null.
This however (from above) works:
The answer that dtsg gave works:
BUT, you need to add an assembly reference to
Go to your Solution Explorer and right click on References and select Add reference. Select the Assemblies tab and search for Configuration.
Here is an example of my App.config:
Which you can get in the following way:
Read From Config :
You'll need to add a reference to Config
Save to Config :
Give this a go: