The Labs \ Source Viewer \ SSCLI \ System.Configuration \ NameValueFileSectionHandler

  1. //------------------------------------------------------------------------------
  2. // <copyright file="NameValueFileSectionHandler.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Configuration
  16. {
  17.     using System.Configuration.Internal;
  18.     using System.IO;
  19.     using System.Xml;
  20.     using System.Runtime.Versioning;
  21.    
  22.     /// <internalonly/>
  23.     /// <devdoc>
  24.     /// <para>
  25.     /// This section handler allows &lt;appSettings file="user.config" /&gt;
  26.     /// The file pointed to by the file= attribute is read as if it is
  27.     /// an appSettings section in the config file.
  28.     /// Note: the user.config file must have its root element match the
  29.     /// section referring to it. So if appSettings has a file="user.config"
  30.     /// attribute the root element in user.config must also be named appSettings.
  31.     /// </para>
  32.     /// </devdoc>
  33.     public class NameValueFileSectionHandler : IConfigurationSectionHandler
  34.     {
  35.        
  36.         [ResourceExposure(ResourceScope.None)]
  37.         [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
  38.         public object Create(object parent, object configContext, XmlNode section)
  39.         {
  40.             object result = parent;
  41.            
  42.             // parse XML
  43.             XmlNode fileAttribute = section.Attributes.RemoveNamedItem("file");
  44.            
  45.             result = NameValueSectionHandler.CreateStatic(result, section);
  46.            
  47.             if (fileAttribute != null && fileAttribute.Value.Length != 0) {
  48.                 string filename = null;
  49.                 filename = fileAttribute.Value;
  50.                 IConfigErrorInfo configXmlNode = fileAttribute as IConfigErrorInfo;
  51.                 if (configXmlNode == null) {
  52.                     return null;
  53.                 }
  54.                
  55.                 string configFile = configXmlNode.Filename;
  56.                 string directory = Path.GetDirectoryName(configFile);
  57.                 string sourceFileFullPath = Path.Combine(directory, filename);
  58.                
  59.                 if (File.Exists(sourceFileFullPath)) {
  60.                    
  61.                     ConfigXmlDocument doc = new ConfigXmlDocument();
  62.                     try {
  63.                         doc.Load(sourceFileFullPath);
  64.                     }
  65.                     catch (XmlException e) {
  66.                         throw new ConfigurationErrorsException(e.Message, e, sourceFileFullPath, e.LineNumber);
  67.                     }
  68.                    
  69.                     if (section.Name != doc.DocumentElement.Name) {
  70.                         throw new ConfigurationErrorsException(SR.GetString(SR.Config_name_value_file_section_file_invalid_root, section.Name), doc.DocumentElement);
  71.                     }
  72.                    
  73.                     result = NameValueSectionHandler.CreateStatic(result, doc.DocumentElement);
  74.                 }
  75.             }
  76.            
  77.             return result;
  78.         }
  79.     }
  80. }

Developer Fusion