2
votes

I have a config file named Refer.config which contains the appSettings as:

<?xml version="1.0"?>
<configuration>
<appSettings>
    <add key="Database" value="CouchDB"/>
  </appSettings>
</configuration>

Now in the web.config i have referenced it as:

<appSettings file="C:\Users\312171\Documents\Visual Studio 2008\WebSites\WebSite4\Refer.config">

  </appSettings>

Now in the cs file i m trying to access the key value of appSettings like this:

protected void Button2_Click(object sender, EventArgs e)
    {
        string filetype = ConfigurationManager.AppSettings["Database"];
        testvalue.Text = string.Format("The key value is:",filetype);
    }

But it is not returning the key value... What mistake i have made?? Please help me out..

2
Does your program has sufficient permission to read file? Any exception is it throwing? - Mohan
@Mohan No exception is been thrown.. It jus displays as" The key value is: " - Xavier

2 Answers

3
votes

You must use the configSource attribute if you are on .net 2.0 or above.

<appSettings configSource="Refer.config">
</appSettings>

Note that the path must be relative.

File is supposed to take a relative path

File specifies a relative path to an external file containing custom application configuration settings. The specified file contains the same kind of settings that are specified in the , , and elements and uses the same key/value pair format as those elements.

The path specified is relative to the main configuration file. For a Windows Forms application, this would be the binary folder (such as /bin/debug), not the location of the application configuration file. For Web Forms applications, the path is relative to the application root, where the web.config file is located.

Note that the runtime ignores the attribute if the specified file can not be found.

0
votes

Got my answer by trying like this in web.config...

<appSettings file="Refer.config"/>