Change web.config settings in code

im using a third-party download control and there are several 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 the code, for example, I want to make showcancelbutton = "false"

how to do it

+3
source share
2 answers

Since this is the web application you want to change, I would go with the WebConfigurationManager.

If the configuration value that you are going to change is in a separate section, you need to get this 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 web.config changes.

A more detailed article explaining this is available here .

+5

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

0

Source: https://habr.com/ru/post/1723214/


All Articles