I am trying to do the exact same thing mentioned on this recent previous question. In essence, here's the case (which is exactly my same situation):
My plan is to have these (appSettings) in their own file (Settings.config), to which I will grant modify permissions to the web process user account, and store all editable settings in this file (e.g. forum title, description, etc).
The problem is that the solution accepted in that question doesn't work for me because instead of saving the appSettings in the separate .config file, when I issue the config.Save(ConfigurationSaveMode.Minimal, false)
command, it replicates all the appSettings of the separate file into the appSettings section of the main web.config file (with the new changes). Here's my final code (in vb.net):
Public Shared Function GetAppSetting(ByVal setting As String) As String
Dim config As System.Configuration.Configuration = _
WebConfigurationManager.OpenWebConfiguration("~")
Return config.AppSettings.Settings(setting).Value
End Function
Public Shared Sub SetAppSetting(ByVal setting As String, ByVal value As String)
Dim config As System.Configuration.Configuration = _
WebConfigurationManager.OpenWebConfiguration("~")
config.AppSettings.Settings(setting).Value = value
config.Save(ConfigurationSaveMode.Minimal, False)
ConfigurationManager.RefreshSection("appSettings")
End Sub
Basically I can't see where I would be indicating that I want the settings to be saved on the separate file instead of on the web.config which is where they are stored by default. Oh and by the way, I had to add the 'file=' attribute on the appSettings section of the web.config so that the Settings.config appSettings would be actually taken into account. Without that attribute the above code doesn't read the separate .config file settings. Here's a snapshot of my web.config appSettings section:
<appSettings file="Settings.config">
<add key="RestartApp" value="-1" />
</appSettings>
And here's the entire contents of my Settings.config file:
<appSettings>
<add key="AppTitle" value="MVC Web Access" />
<add key="DefaultWebpage" />
<add key="CustomCSS" />
<add key="TktsEmailTo" value="[email protected]" />
<add key="EmailFrom" value="[email protected]" />
<add key="EmailFromSMTP" value="mail.email.com" />
<add key="EmailFromPW" value="fakePassword" />
</appSettings>
So instead of ending up with modified settings on my Settings.config file after the .save command, my appSettings section on the web.config file ends up like this (and the Settings.config file remains untouched):
<appSettings file="Settings.config">
<add key="RestartApp" value="-1" />
<add key="AppTitle" value="New title" />
<add key="DefaultWebpage" value="index.aspx" />
<add key="CustomCSS" />
<add key="TktsEmailTo" value="[email protected]" />
<add key="EmailFrom" value="[email protected]" />
<add key="EmailFromSMTP" value="mail.email.com" />
<add key="EmailFromPW" value="NewFakePassword" />
</appSettings>
Just double-checked - the only difference I can see is that I'm using
<appSettings configSource="Settings.config"/>
Here is the code I'm using now, which is working and saving settings to my separate settings file (Settings.config):
var config = WebConfigurationManager.OpenWebConfiguration("~"); foreach (var key in collection.Keys) { if (config.AppSettings.Settings[key.ToString()] != null) { config.AppSettings.Settings[key.ToString()].Value = collection[key.ToString()]; } } config.Save(ConfigurationSaveMode.Minimal, false); ConfigurationManager.RefreshSection("appSettings");
What happens if you use configSource
on your appSettings
key?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With