如何模拟.config文件的appSettings部分的文件属性

时间:2016-02-29 07:43:23

标签: c# app-config

Microsoft允许instructions for how to use the file attribute使.config个文件的appSettings部分引用另一个文件:例如:

<appSettings file="c:\commonSettings.config">
    <add key="myAppSpecificSetting" value="Setting1" />
</appSettings>

但是,这是implemented specifically for only appSettings并且不是某些常规.config文件功能。

现在,我已经实现了自己的ConfigurationSection,并希望能够像file=一样指定appSettings。除了简单地创建属性以匹配属性并检索文件名之外,我如何在自定义configSection中实际重新创建/模拟此功能?

1 个答案:

答案 0 :(得分:1)

谢天谢地,我们有the source code now。看起来它只是由AppSettingsSection通过读取文件系统实现的,没有什么特别的,你可以在你自己的配置部分重用。

protected internal override void DeserializeElement(XmlReader reader, bool serializeCollectionKey) 
{
    string ElementName = reader.Name;

    base.DeserializeElement(reader, serializeCollectionKey);
    if ((File != null) && (File.Length > 0)) {
        string sourceFileFullPath;
        string configFileDirectory;
        string configFile;

        // Determine file location
        configFile = ElementInformation.Source;

        if (String.IsNullOrEmpty(configFile)) {
            sourceFileFullPath = File;
        }
        else {
            configFileDirectory = System.IO.Path.GetDirectoryName(configFile);
            sourceFileFullPath = System.IO.Path.Combine(configFileDirectory, File);
        }

        if (System.IO.File.Exists(sourceFileFullPath)) {
            int lineOffset = 0;
            string rawXml = null;

            using (Stream sourceFileStream = new FileStream(sourceFileFullPath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                using (XmlUtil xmlUtil = new XmlUtil(sourceFileStream, sourceFileFullPath, true)) {
                    if (xmlUtil.Reader.Name != ElementName) {
                        throw new ConfigurationErrorsException(
                                SR.GetString(SR.Config_name_value_file_section_file_invalid_root, ElementName),
                                xmlUtil);
                    }

                    lineOffset = xmlUtil.Reader.LineNumber;
                    rawXml = xmlUtil.CopySection();

                    // Detect if there is any XML left over after the section
                    while (!xmlUtil.Reader.EOF) {
                        XmlNodeType t = xmlUtil.Reader.NodeType;
                        if (t != XmlNodeType.Comment) {
                            throw new ConfigurationErrorsException(SR.GetString(SR.Config_source_file_format), xmlUtil);
                        }

                        xmlUtil.Reader.Read();
                    }
                }
            }

            ConfigXmlReader internalReader = new ConfigXmlReader(rawXml, sourceFileFullPath, lineOffset);
            internalReader.Read();
            if (internalReader.MoveToNextAttribute()) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Config_base_unrecognized_attribute, internalReader.Name), (XmlReader)internalReader);
            }

            internalReader.MoveToElement();

            base.DeserializeElement(internalReader, serializeCollectionKey);
        }
    }
}