views:

209

answers:

2

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

A: 

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

Michel van Engelen
+1  A: 

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.

PHeiberg