.net 我可以在一个通用app.config文件中共享一些配置部分吗。网

.net 我可以在一个通用app.config文件中共享一些配置部分吗。网,.net,app-config,.net,App Config,我有两个项目共享一些appSettinsg和config部分。比如说: ProjectA有自己的app.config,其中包含其独特的设置/配置部分 项目B同上 现在,在每个app.config中,我想指向第三个共享的.config文件,其中包含一些常见的appSettings和config部分,供ProjectA和B使用 我知道我可以使用configSource属性为每个configSection引用一个外部文件,但是,从实验来看,这种方法只能为每个外部文件保存一个configSectio

我有两个项目共享一些appSettinsg和config部分。比如说:

  • ProjectA有自己的app.config,其中包含其独特的设置/配置部分
  • 项目B同上
现在,在每个app.config中,我想指向第三个共享的.config文件,其中包含一些常见的appSettings和config部分,供ProjectA和B使用

我知道我可以使用configSource属性为每个configSection引用一个外部文件,但是,从实验来看,这种方法只能为每个外部文件保存一个configSection(通过定义configSection作为其根元素)。为了方便起见,我希望“通用”文件包含多个文件


这可能吗?

您可以将所有设置分组到自己的自定义配置部分。当然,您可以使用上面提到的
configSource
属性将其一起移动到不同的文件中

对于AppSettings,您的自定义配置部分可以使用函数将自己的值合并到普通AppSettings(即
NameValueCollection
)中。这样,您根本不需要更改客户端代码

另一方面,这里是我的一些基类,我使用它向大多数自定义元素添加“externalConfigSource”属性,以允许对我的一些子元素进行进一步的文件拆分(尽管这可能是您试图避免的):


您可以将所有设置分组到自己的自定义配置部分。当然,您可以使用上面提到的
configSource
属性将其一起移动到不同的文件中

对于AppSettings,您的自定义配置部分可以使用函数将自己的值合并到普通AppSettings(即
NameValueCollection
)中。这样,您根本不需要更改客户端代码

另一方面,这里是我的一些基类,我使用它向大多数自定义元素添加“externalConfigSource”属性,以允许对我的一些子元素进行进一步的文件拆分(尽管这可能是您试图避免的):


刚才看到您在上面的评论,许多节已经是自定义配置节,如果是这样,您可以扩展以使用此“externalConfigSource”代码,但您必须将读取器移动到文件中的适当位置以找到元素的开头。刚才看到您在上面的评论,许多节已经是自定义配置节,如果是这样,您可以扩展以使用此“externalConfigSource”代码,但您必须将读取器移动到文件中的适当位置以定位元素的开头。
public class BaseConfigurationElement : ConfigurationElement
{
    protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
    {
        var fileSource = reader.GetAttribute("externalConfigSource");
        if (!String.IsNullOrEmpty(fileSource))
        {
            var file = new FileInfo(Path.Combine(AppDomainExtensions.ConfigurationFilePath(), fileSource));
            if (file.Exists)
            {
                using (var fileReader = file.OpenRead())
                {
                    var settings = new XmlReaderSettings(){ CloseInput = true, IgnoreProcessingInstructions = true, IgnoreWhitespace = true, IgnoreComments = true};
                    using (var fileXmlReader = XmlReader.Create(fileReader, settings))
                    {
                        var atStart = fileXmlReader.IsStartElement();
                        base.DeserializeElement(fileXmlReader, serializeCollectionKey);
                    }
                }
                reader.Skip();
            }
            else
            {
                throw new ConfigurationErrorsException("The file specified in the externalConfigSource attribute cannot be found", reader);
            }
        }
        else
        {
            base.DeserializeElement(reader, serializeCollectionKey);
        }
    }

    protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
    {
        if (name == "externalConfigSource")
        {
            return true; // Indicate that we do know it...
        }
        return base.OnDeserializeUnrecognizedAttribute(name, value);
    }
}

public static class AppDomainExtensions
{
    public static string ConfigurationFilePath()
    {
        return ConfigurationFilePath(AppDomain.CurrentDomain);
    }

    // http://stackoverflow.com/questions/793657/how-to-find-path-of-active-app-config-file
    public static string ConfigurationFilePath(this AppDomain appDomain)
    {
        return Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    }
}