tags:

views:

42

answers:

1

I'm using the Microsoft.Web.Administration library to configure a web.config on a remote machine. I've been able to read sections and add new items for the most part using the GetWebConfiguration method on the Application object, but I've run into a snag with appSettings. I would like to be able to read and write to the appsettings section but I can't seem to find a way to do this and have not found a good example on-line.

Is this supported using Microsoft.Web.Administration? If not, is there another elegant solution? The solution has to work remotely.

+1  A: 

Yes it is, you should be able to do as below, note that I generated the code using the IIS Configuration Editor, see: http://blogs.iis.net/bills/archive/2008/06/01/how-do-i-script-automate-iis7-configuration.aspx

using(ServerManager mgr = ServerManager.OpenRemote("Some-Server")) {

  Configuration config = mgr.GetWebConfiguration("site-name", "/test-application");

  ConfigurationSection appSettingsSection = config.GetSection("appSettings");

  ConfigurationElementCollection appSettingsCollection = appSettingsSection.GetCollection();

  ConfigurationElement addElement = appSettingsCollection.CreateElement("add");
  addElement["key"] = @"NewSetting1";
  addElement["value"] = @"SomeValue";
  appSettingsCollection.Add(addElement);

  serverManager.CommitChanges();
}
CarlosAg
Thanks! That was very helpful, especially the link to the blog post.
BenR