Modify Web.Config Programmatically

The gives me a headache for couple of days, and I finally solve this puzzle which everyone else gets no trouble in doing it.

First of all, let me explain some background knowledge on web.config which is the reason why I got into trouble with.

There is a hierarchy on configuration, machine.config->web.config(root)->web.config(app)->web.config(sub)

the latter ones override the previous ones.

machine.config and web.config(root) can be found at

%WINDIR%\Microsoft.NET\Framework\[version]\CONFIG\

[version] is your .NET framework version.

Now, let’s start to dicuss how to modify web.config(app).

System.Configuration.Configuration l_webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(m_webConfig, m_website, null, Server.MachineName, l_iisUserName, l_iisPassword);
AppSettingsSection l_appSetting = l_webConfig.AppSettings;
ConnectionStringsSection l_connSetting = l_webConfig.ConnectionStrings;+
//Get a setting from appSettings section
string l_appValue = l_appSetting.Settings["key"].value
//Get a connection string from connectionStringsSection
string l_connValue = l_connSetting.ConnectionStrings["ConnectionString"].ConnectionString
//Add a new appSetting
l_appSetting.Settings.Add("key","value")
//Add a new connection string
l_connSetting.ConnectionStrings.Add(new ConnectionStringSettings("name","value"))
//save it
l_webConfig.save(ConfigurationSaveMode.Minimal);

The first line open up a web.config, I am using one of its overload only.

The first parameter is the path where your application web.config locate, usually is “~”.

The second parameter is your website name, default value is “Default Web site”.

The third one is subLocationPath, I am not sure what this is. this might be for web.config(sub).

The fourth one is host name. 5th and 6th parameters is the server administrator account/password.

After modifying your configuration, save it back to the database.

There are three save mode which is self-explanatory.

 

I get two problems when trying to modify web.config.

The first one is, it will tells you that AllowLocation=false, can not modify web.config.

When this happens, set it to true in your machine.config.

Another one is, when you are not using account/password to open web.config.

If you are running in Visual Studio, it will use ASP.NET account to open it.

If you are running in IIS, it will use IIS account or NETWORK SERVICES account.

Make sure the account that your are using has the permission to modify web.config file.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s