5

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..

4
  • 4
    There is no app.config in asp.net, you need to use web.config instead. Commented May 13, 2011 at 9:17
  • If you're running ASP.NET you need to use Web.config, not App.config Commented May 13, 2011 at 9:17
  • Anyway, don't load web.config/app.config as XmlDocument. Use built-in infrastructure members instead. Commented May 13, 2011 at 9:18
  • how can this be done???? Commented May 13, 2011 at 9:20

3 Answers 3

10

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"];
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, but in an ASP.NET application, you have to use the web.config file, not the app.config file.
yes i have used web.config and rename it to app.config file. is this possible....
No. It needs to be named WEB.CONFIG. The .net configuration environment uses a set of conventions where files need to be named certain things for the ConfigurationManager to work.
but i get from certain website that app.config is used to do some personel settings in .config file
@percy: In ASP.NET, default config is called Web.config, any other - is custom and optional. So you can name it App.config (like in WinForms) or anyhow else, Foo.config or Config\Bar.config. Custom loader (ConfigurationManager.OpenExeConfiguration()) is required to load and read such file.
6

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

1 Comment

if i had to use some personel setting then i have to give it in web.config file, inside "<appsettings>".
1

If you want to use ConfigurationManager.AppSettings inside a web application, you have to put your AppSettings section in web.config.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.