BenR
BenR

Reputation: 12276

How can I manipulate appsettings in a web.config on a remote machine using Microsoft.Web.Administration?

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.

Upvotes: 0

Views: 2826

Answers (1)

Carlos Aguilar Mares
Carlos Aguilar Mares

Reputation: 13581

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();
}

Upvotes: 3

Related Questions