Mishigen
Mishigen

Reputation: 1261

change web.config settings in code behind

i m using a third party upload control and there r few settings in web.config

<uploadSettings allowedFileExtensions=".pdf,.xls,.doc,.zip,.rar,.jpg" scriptPath="upload_scripts" imagePath="" cssPath="upload_styles" enableManualProcessing="true" showProgressBar="true" showCancelButton="true"/>

now i want to change these settings from code behind e.g i want to make showcancelbutton="false"

how do i do that

Upvotes: 3

Views: 4402

Answers (2)

PHeiberg
PHeiberg

Reputation: 29801

Since it's a web application you want to change I'd go with the WebConfigurationManager.

If the configuration value you are about to change is in a separate section you need to get that section first:

var myConfiguration = (Configuration)WebConfigurationManager.OpenWebConfiguration("~");
var section = (MySectionTypeHere)myConfiguration.GetSection("system.web/mySectionName");
//Change your settings here
myConfiguration.Save();

Keep in mind that the web application will be restarted each time you change the web.config.

An article explaining it more in detail is available here.

Upvotes: 5

Michel van Engelen
Michel van Engelen

Reputation: 2879

You can use the Configuration class which resides in system.configuration.

string configLocation = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string configPath = Path.Combine(configLocation, "yourAppName");
Configuration configFile = ConfigurationManager.OpenExeConfiguration(configPath); configFile.AppSettings.Settings["TheSettingYouWantToChange"].Value = "NewValue"; configFile.Save(ConfigurationSaveMode.Modified);

Upvotes: 0

Related Questions