1

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";            
    }
Rob
  • 45,296
  • 24
  • 122
  • 150
neha dhage
  • 11
  • 2
  • Could you add the full exception details please? – MPritchard Jul 29 '10 at 12:56
  • And some information about the folder-structure of your web-project... is it running under IIS or dev-server? Normally the web.config should be laying right under AppDomain.CurrentDomain.BaseDirectory... On an IIS your path would result in "C:\web.config" if you have installed your application in "C:\inetpub\wwwroot"... – OlafW Jul 29 '10 at 13:00
  • @MPritch - I suspect that 'Could not find file' is the exception message, almost certainly because `..\\..\\` is too far down the directory tree, given the fact that it's quite unlikely that the OP is storing their web app in C:\Users\maxnet25\Web.config. More likely is C:\Users\maxnet25\Documents\MyWebApp\Web.config, which would be where the code would look if not for the ..\\..\\ =) – Rob Jul 29 '10 at 13:02

5 Answers5

4

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.

Rob
  • 45,296
  • 24
  • 122
  • 150
2

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.

Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
  • When dealing with appSettings, you can avoid the restart by storing appSettings in an external config file. See http://msdn.microsoft.com/en-us/library/ackhksh7.aspx. – kbrimington Jul 29 '10 at 13:06
  • @kbrimington, although if you're running in a "web garden" scenario, this would mean that other worker processes wouldn't pick up on your configuration changes. Just worth bearing in mind =) – Rob Jul 29 '10 at 13:07
1

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.

Community
  • 1
  • 1
kbrimington
  • 25,142
  • 5
  • 62
  • 74
0

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

Wallace Breza
  • 4,898
  • 1
  • 28
  • 33
0

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.

Jaymz
  • 6,140
  • 2
  • 26
  • 30