I am using app.config file to read data from it..
I am loading the app.config file as:
string app_path = HttpContext.Current.Server.MapPath("app.config");
xmlDoc.Load(app_path);
string image_path = ConfigurationManager.AppSettings["Test1"];
and i want to get the value of “Test1”. But the value of test1 is comming “null”..
how can i get the value of “test1” from app.config file..
i created the app.config file as:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Test1" value="My value 1" />
<add key="Test2" value="Another value 2" />
</appSettings>
</configuration>
please help me out..
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Test1" value="My value 1" />
<add key="Test2" value="Another value 2" />
</appSettings>
</configuration>
Code:
string image_path = ConfigurationManager.AppSettings["Test1"];
Method 2
In ASP.NET applications, the default configuration file is named web.config. This is a convention you should probably stick to, that allows you to easily use the ConfigurationManager to access configuration settings.
I suggest having a look at http://en.wikipedia.org/wiki/Web.config as a starting point to learn the ins and outs of basic .NET application configuration in the ASP.NET domain.
You can link configuration files together, by setting the file attribute of configuration sections you want to override: http://www.codeproject.com/KB/dotnet/appsettings_fileattribute.aspx
Method 3
If you want to use ConfigurationManager.AppSettings inside a web application, you have to put your AppSettings section in web.config.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0