views:

826

answers:

2

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>
A: 

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?

Keith Williams
I'll give it a try... I read about this setting but didn't try it because I understood that if used, I would be able to have both an external .config file with separate appSettings and also additional appSettings on the web.config's file corresponding section. I basically wanted to keep that "<add key="RestartApp" value="-1" />" setting in order to give the user an option to restart the application and not have it always restart on an appSettings change. Anyhow, I'll test this option and come back with the response. Thanks.
Rodolfo G.
A: 

//================================================================================= //modificar archivo xxxxxxxxxxxx.vshost.exe.config para que tome los cambios en el config //================================================================================= Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Get the AppSettings section. ConfigurationManager.RefreshSection("appSettings");
AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("appSettings");

string mivalor = appSettingSection.Settings["my_key"].Value;

damian