neha dhage
neha dhage

Reputation: 11

web.config in asp.net

I've got the function for changing the values in web.config but my problem is it is not getting the path of web.config correctly and throwing

"Could not find file 'C:\Users\maxnet25\Web.config'" It was giving error on xmlDoc.Load() function.

My code:

    public void UpdateConfigKey(string strKey, string newValue)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Web.config");
        if (!ConfigKeyExists(strKey))
        {
            throw new ArgumentNullException("Key", "<" + strKey + "> not find in the configuration.");
        }

        XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");
        foreach (XmlNode childNode in appSettingsNode)
        {
            if (childNode.Attributes["key"].Value == strKey)
                childNode.Attributes["value"].Value = newValue;
        }
        xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Web.config");
        xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                 Label1 .Text ="Key Upated Successfullly";            
    }

Upvotes: 1

Views: 1932

Answers (5)

Jaymz
Jaymz

Reputation: 6348

You should use either:

System.Configuration.ConfigurationManager

for app.config files, or:

System.Web.Configuration.WebConfigurationManager

for web.config files.

You can actually use System.Configuration.ConfigurationManager with web.config files as well, and to be honest, I'm not actually sure if there's any benefit for using one over the other.

But either way, you should not be using the Xml namespaces and writing/modifying the raw XML.

Upvotes: 0

Rob
Rob

Reputation: 45789

What error messsage is being given?

Either way, you're not really going about modifying web.config in the right way. You should probably take a look at the System.Configuration.ConfigurationManager class as this provides programmatic access to the web.config file in a structured manner. Note that to access this class you need to add a reference to System.Configuration.dll to your project to bring the ConfigurationManager into scope.

If you look at the example code for the GetSection method, it shows how to create/add settings in the appSettings section of a .net config file, so that example should be enough to get you where you want to go.

If you definately want to use this approach to manipulate your web.config file, I suspect that:

AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Web.config")

is incorrect, based on the path that you've shown in the error message. Try removing the ..\..\ and seeing if that works. AppDomain.CurrentDomain.BaseDirectory should be pointing at the location of your web.config file without modification.

Upvotes: 4

kbrimington
kbrimington

Reputation: 25692

Try using Server.MapPath() to resolve the location of your web.config. If you're in a page, Server is one of the page properties. If not, you can find it in HttpContext.Current.

As an example...

HttpContext.Current.Server.MapPath("~/web.config")

...should return the physical path to the web.config at the top of your web application.

Now, you're probably much better off using the WebConfigurationManager, as shown in this post. The approach is much cleaner, but requires a reference to System.Configuration.

Upvotes: 1

Timothy S. Van Haren
Timothy S. Van Haren

Reputation: 8966

Assuming this is indeed an ASP.NET website, instead of this:

AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Web.config"

Use this:

HttpContext.Current.Server.MapPath("~/Web.config")

On a side note, please be aware that anytime you make a change to web.config, your web application restarts. You might not need to worry about that depending on what your web app does though.

Upvotes: 2

Wallace Breza
Wallace Breza

Reputation: 4948

Have you added a web.config to your web site?

Upvotes: 0

Related Questions