My XML looks like this and the filename is web.config
<?xml version="1.0"?> <configuration> <appSettings> <add key="configFile" value="IIS.config"/> <add key="RialtoDomain" value="ASNC_AUDITORS"/> </appSettings> <system.serviceModel> .... </system.serviceModel> </configuration>
In the code when I read like this
String path = ConfigurationSettings.AppSettings["configFile"];
I am getting a null value. No exception is thrown. Is this the right way to do it?
AppSetting section in the configuration file is a section that allows us to keep configurable and application wide settings (for e.g.: ConnectionString) that an application requires in order to perform the tasks properly. This helps in easy maintenance and deployment of the application.
To access these values, there is one static class named ConfigurationManager, which has one getter property named AppSettings. We can just pass the key inside the AppSettings and get the desired value from AppSettings section, as shown below.
Since you're accessing a web.config
you should probably use
using System.Web.Configuration; WebConfigurationManager.AppSettings["configFile"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With