mardi 10 février 2015

Change web.config appSetting with Reflection

I am using an API which I cannot change the source code but I have access to the source code to see what's going on. In the API, there is a call which gets a value from the web.config:



String myVal = ConfigurationManager.AppSettings["Somekey"];


The problem is the value for App Setting "SomeKey" needs to change. I tried the following:



Configuration myConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
myConfiguration.AppSettings.Settings["Somekey"].Value = "SomeValue";
myConfiguration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(myConfiguration.AppSettings.SectionInformation.Name);


But that will modify the web.config, save the file and reset the App Pool cache.


Is there a way using reflection to change an appSetting. I tried the following:



Type configType = typeof(ConfigurationManager);
FieldInfo appSettings = configType.GetField("AppSettings", BindingFlags.Static | BindingFlags.Public);


configType.GetFields() does not return any fields.






Aucun commentaire:

Enregistrer un commentaire