本文介绍了在不同的config文件的AppSettings未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做这个最近提到的同样的事情$p$pvious问题。从本质上说,这里的情况下(这正是我一样的情况):

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@email.com" />
    <add key="EmailFrom" value="email@email.com" />
    <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="newemail@email.com" />
    <add key="EmailFrom" value="newemail@email.com" />
    <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?

这篇关于在不同的config文件的AppSettings未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:55