Reading settings from separate config file

女生的网名这么多〃 提交于 2019-11-28 09:27:57

In each section, you can define configSource, which can point to an external file path. Here's a simple example:

<connectionStrings configSource="myConnectionStrings.Config" />
<appSettings configSource="myAppSettings.Config" />

Just make sure not to use .xml file extension since it can be viewed in a browser. .config will not be served by the web server.

Because your config sections are still defined in the web.config (thus pointing to external files), you can access this information via the normal routes (WebConfigurationManager.AppSettings, WebConfigurationManager.GetSection, ConfigurationManager, or custom section handlers as needed)

I used a configuration helper in a shared DLL, and an app.config file in the DLL that uses the Settings.Properties.Default stuff by editing the project and setting the settings tab. It appears that value isn't read unless you recompile, and resync the app.config (in the dll) with the project settings.

This works for me. I don't remember where I got the inspiration. I just include this class in a shared project somewhere. allows any DLL to call its own settings, which allows you to change the dllFile.dll.config entries. I use this for connection strings. The caveat is that in this method, the connection string has to be a type string, and not the special connection string.

using System;
using System.Configuration;

namespace Shared
{
    public static class ConfigurationHelper
    {
        public static string GetConfigValue(string keyName)
        {
            string codebase = System.Reflection.Assembly.GetCallingAssembly().CodeBase;  
            Uri p = new Uri(codebase);
            string localPath = p.LocalPath.ToLowerInvariant();
            string executingFilename = System.IO.Path.GetFileNameWithoutExtension(localPath);
            string sectionGroupName = "applicationSettings";
            string sectionName = executingFilename + ".Properties.Settings";
            string configName = localPath + ".config";
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = configName;
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            ConfigurationSectionGroup group = config.GetSectionGroup(sectionGroupName);
            ClientSettingsSection section = null;
            foreach (ClientSettingsSection sect in group.Sections)
            {
                if (sect.SectionInformation.Name.Equals(sectionName, StringComparison.InvariantCultureIgnoreCase))
                {
                    section = sect;
                    break;
                }
            }
            SettingElement elem = section.Settings.Get(keyName);
            if (elem == null)
                return "";
            else
                return elem.Value.ValueXml.InnerText.Trim();
        }
    }
}

//in DLL
void foo()
{
    var str = ConfigurationHelper.GetSetting("ConnectionStringProd");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!