Common. NET Function Code summary,. net code Summary
1. Modify the Web. config at the specified file location on the Web
The WebConfigurationManager class must be used here, but the WebConfigurationFileMap class must be used to specify the file location. See the Code:
Long appId = 123; // modify the website configuration file var configFile = new FileInfo (configFilePath); var vdm = new VirtualDirectoryMapping (configFile. directoryName, true, configFile. name); var wcfm = new WebConfigurationFileMap (); wcfm. virtualDirectories. add ("/", vdm); var config = WebConfigurationManager. openMappedWebConfiguration (wcfm, "/", siteName); etettingssection appSection = (AppSettingsSection) config. getSection ("appSettings"); appSection. settings ["abc"]. value = "system tag"; appSection. settings ["appId"]. value = appId. toString (); config. save ();
The above code references stackoverflow, but OpenMappedWebConfiguration must specify 3rd parameters. Otherwise, an error is returned. However, siteName does not need to correspond to the site name of IIS. You can write one at will.