TechGuy
TechGuy

Reputation: 4570

Change a REMOTE web.config programmatically in C#

I have two web project hosted on same server that is One site User & Admin Projects.I want to dynamically change the values of App Setting (Key of app setting)

I know how to change the value web.config value programmatically in Same Project.But how different project,

System.Configuration.ExeConfigurationFileMap configFile = new System.Configuration.ExeConfigurationFileMap();
configFile.ExeConfigFilename = "ConsoleTester.exe.config";  //name of your config file, can be from your app or external
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, System.Configuration.ConfigurationUserLevel.None);
System.Configuration.KeyValueConfigurationCollection settings = config.AppSettings.Settings;
settings["DD"].Value = "007_Access";
config.Save();

Upvotes: 0

Views: 1292

Answers (3)

H.Mikhaeljan
H.Mikhaeljan

Reputation: 813

You can make a app_offline.htm file which will take your site down. All you have to do is place the file in the root directory of your website and IIS will do the rest for you. You can add a maintenance message on the app_offline.htm and do maintenance while the users will see only the app_offline.htm page

If you would really wanna go programmatically. You can rename the file to something else when its not needed and rename it back to app_offline.htm when you wanna take the site down.

Upvotes: 0

ste-fu
ste-fu

Reputation: 7454

In IIS 7+ there is a C# API that allows you to create applications, and modify configs.

The documentation is here Microsoft.Web.Administration

I think this code should get you there abouts:

   var serverManager = new ServerManager();
   var site = serverManager.Sites["SiteName"];
   var app = site.Applications["MyAppName"];
   var config = app.GetWebConfiguration();
   var section = config.GetSection("appSettings");
   var element = section.GetChildElement("mySettingKey");
   element.SetAttributeValue("value", "MyNewValue");
   serverManager.CommitChanges();

Upvotes: 1

Ivan Parvareshi
Ivan Parvareshi

Reputation: 41

What I get of your problem is, You have one codebase and you need to serve two application instance with different functionalities.

First of all its not good idea to modify your web.config at runtime.

To have two application instance based on one codebase with separated web.config, you can use virtual directory feature of IIS. create two web-application on IIS with two web.config(separated directory) and then define a virtual directory pointed to your codebase.

Upvotes: 1

Related Questions