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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="Configuration.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. using ClassConfiguration = System.Configuration.Configuration;
  16. using System.Collections;
  17. using System.Configuration;
  18. using System.Configuration.Internal;
  19. using System.IO;
  20. using System.Reflection;
  21. using System.Security;
  22. using System.Security.Permissions;
  23. using System.Threading;
  24. namespace System.Configuration
  25. {
  26.    
  27.     //
  28.     // An instance of the Configuration class represents a single level
  29.     // in the configuration hierarchy. Its contents can be edited and
  30.     // saved to disk.
  31.     //
  32.     // It is not thread safe for writing.
  33.     //
  34.     public sealed class Configuration
  35.     {
  36.         private Type _typeConfigHost;
  37.         // type of config host
  38.         private object[] _hostInitConfigurationParams;
  39.         // params to init config host
  40.         private IInternalConfigRoot _configRoot;
  41.         // root of this configuration hierarchy
  42.         private MgmtConfigurationRecord _configRecord;
  43.         // config record for this level in the hierarchy
  44.         private ConfigurationSectionGroup _rootSectionGroup;
  45.         // section group for the root of all sections
  46.         private ConfigurationLocationCollection _locations;
  47.         // support for ConfigurationLocationsCollection
  48.         private ContextInformation _evalContext;
  49.         // evaluation context
  50.         internal Configuration(string locationSubPath, Type typeConfigHost, params object[] hostInitConfigurationParams)
  51.         {
  52.             _typeConfigHost = typeConfigHost;
  53.             _hostInitConfigurationParams = hostInitConfigurationParams;
  54.            
  55.             _configRoot = new InternalConfigRoot();
  56.            
  57.             IInternalConfigHost configHost = (IInternalConfigHost)TypeUtil.CreateInstanceWithReflectionPermission(typeConfigHost);
  58.            
  59.             // Wrap the host with the UpdateConfigHost to support SaveAs.
  60.             IInternalConfigHost updateConfigHost = new UpdateConfigHost(configHost);
  61.            
  62.             _configRoot.Init(updateConfigHost, true);
  63.            
  64.             //
  65.             // Set the configuration paths for this Configuration.
  66.             // We do this in a separate step so that the WebConfigurationHost
  67.             // can use this object's _configRoot to get the <sites> section,
  68.             // which is used in it's MapPath implementation.
  69.             //
  70.             string configPath;
  71.             string locationConfigPath;
  72.             configHost.InitForConfiguration(ref locationSubPath, out configPath, out locationConfigPath, _configRoot, hostInitConfigurationParams);
  73.            
  74.             if (!String.IsNullOrEmpty(locationSubPath) && !updateConfigHost.SupportsLocation) {
  75.                 throw ExceptionUtil.UnexpectedError("Configuration::ctor");
  76.             }
  77.            
  78.             if (String.IsNullOrEmpty(locationSubPath) != String.IsNullOrEmpty(locationConfigPath)) {
  79.                 throw ExceptionUtil.UnexpectedError("Configuration::ctor");
  80.             }
  81.            
  82.             // Get the configuration record for this config file.
  83.             _configRecord = (MgmtConfigurationRecord)_configRoot.GetConfigRecord(configPath);
  84.            
  85.             //
  86.             // Create another MgmtConfigurationRecord for the location that is a child of the above record.
  87.             // Note that this does not match the resolution hiearchy that is used at runtime.
  88.             //
  89.             if (!String.IsNullOrEmpty(locationSubPath)) {
  90.                 _configRecord = MgmtConfigurationRecord.Create(_configRoot, _configRecord, locationConfigPath, locationSubPath);
  91.             }
  92.            
  93.             //
  94.             // Throw if the config record we created contains global errors.
  95.             //
  96.             _configRecord.ThrowIfInitErrors();
  97.         }
  98.        
  99.         //
  100.         // Create a new instance of Configuration for the locationSubPath,
  101.         // with the initialization parameters that were used to create this configuration.
  102.         //
  103.         internal Configuration OpenLocationConfiguration(string locationSubPath)
  104.         {
  105.             return new Configuration(locationSubPath, _typeConfigHost, _hostInitConfigurationParams);
  106.         }
  107.        
  108.         // public properties
  109.         public AppSettingsSection AppSettings {
  110.             get { return (AppSettingsSection)GetSection("appSettings"); }
  111.         }
  112.        
  113.         public ConnectionStringsSection ConnectionStrings {
  114.             get { return (ConnectionStringsSection)GetSection("connectionStrings"); }
  115.         }
  116.        
  117.         public string FilePath {
  118.             get { return _configRecord.ConfigurationFilePath; }
  119.         }
  120.        
  121.         public bool HasFile {
  122.             get { return _configRecord.HasStream; }
  123.         }
  124.        
  125.         public ConfigurationLocationCollection Locations {
  126.             get {
  127.                 if (_locations == null) {
  128.                     _locations = _configRecord.GetLocationCollection(this);
  129.                 }
  130.                
  131.                 return _locations;
  132.             }
  133.         }
  134.        
  135.         public ContextInformation EvaluationContext {
  136.             get {
  137.                 if (_evalContext == null) {
  138.                     _evalContext = new ContextInformation(_configRecord);
  139.                 }
  140.                
  141.                 return _evalContext;
  142.             }
  143.         }
  144.        
  145.         public ConfigurationSectionGroup RootSectionGroup {
  146.             get {
  147.                 if (_rootSectionGroup == null) {
  148.                     _rootSectionGroup = new ConfigurationSectionGroup();
  149.                     _rootSectionGroup.RootAttachToConfigurationRecord(_configRecord);
  150.                 }
  151.                
  152.                 return _rootSectionGroup;
  153.             }
  154.         }
  155.        
  156.         public ConfigurationSectionCollection Sections {
  157.             get { return RootSectionGroup.Sections; }
  158.         }
  159.        
  160.         public ConfigurationSectionGroupCollection SectionGroups {
  161.             get { return RootSectionGroup.SectionGroups; }
  162.         }
  163.        
  164.         // public methods
  165.         public ConfigurationSection GetSection(string sectionName)
  166.         {
  167.             ConfigurationSection section = (ConfigurationSection)_configRecord.GetSection(sectionName);
  168.            
  169.             return section;
  170.         }
  171.        
  172.         public ConfigurationSectionGroup GetSectionGroup(string sectionGroupName)
  173.         {
  174.             ConfigurationSectionGroup sectionGroup = _configRecord.GetSectionGroup(sectionGroupName);
  175.            
  176.             return sectionGroup;
  177.         }
  178.        
  179.         // NamespaceDeclared
  180.         //
  181.         // Is the namespace declared in the file or not?
  182.         //
  183.         // ie. xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"
  184.         // (currently this is the only one we allow)
  185.         //
  186.         // get - Return if it was declared in the file.
  187.         // set - Set if we should save the namespace or not
  188.         //
  189.         public bool NamespaceDeclared {
  190.             get { return _configRecord.NamespacePresent; }
  191.             set { _configRecord.NamespacePresent = value; }
  192.         }
  193.        
  194.         public void Save()
  195.         {
  196.             SaveAsImpl(null, ConfigurationSaveMode.Modified, false);
  197.         }
  198.        
  199.         public void Save(ConfigurationSaveMode saveMode)
  200.         {
  201.             SaveAsImpl(null, saveMode, false);
  202.         }
  203.        
  204.         public void Save(ConfigurationSaveMode saveMode, bool forceSaveAll)
  205.         {
  206.             SaveAsImpl(null, saveMode, forceSaveAll);
  207.         }
  208.        
  209.         public void SaveAs(string filename)
  210.         {
  211.             SaveAs(filename, ConfigurationSaveMode.Modified, false);
  212.         }
  213.        
  214.         public void SaveAs(string filename, ConfigurationSaveMode saveMode)
  215.         {
  216.             SaveAs(filename, saveMode, false);
  217.         }
  218.        
  219.         public void SaveAs(string filename, ConfigurationSaveMode saveMode, bool forceSaveAll)
  220.         {
  221.             if (String.IsNullOrEmpty(filename)) {
  222.                 throw ExceptionUtil.ParameterNullOrEmpty("filename");
  223.             }
  224.            
  225.             SaveAsImpl(filename, saveMode, forceSaveAll);
  226.         }
  227.        
  228.         private void SaveAsImpl(string filename, ConfigurationSaveMode saveMode, bool forceSaveAll)
  229.         {
  230.             if (String.IsNullOrEmpty(filename)) {
  231.                 filename = null;
  232.             }
  233.             else {
  234.                 filename = System.IO.Path.GetFullPath(filename);
  235.             }
  236.            
  237.             if (forceSaveAll) {
  238.                 ForceGroupsRecursive(RootSectionGroup);
  239.             }
  240.            
  241.             _configRecord.SaveAs(filename, saveMode, forceSaveAll);
  242.         }
  243.        
  244.         // Force all sections and section groups to be instantiated.
  245.         private void ForceGroupsRecursive(ConfigurationSectionGroup group)
  246.         {
  247.             foreach (ConfigurationSection configSection in group.Sections) {
  248.                 // Force the section to be read into the cache
  249.                 ConfigurationSection section = group.Sections[configSection.SectionInformation.Name];
  250.             }
  251.            
  252.             foreach (ConfigurationSectionGroup sectionGroup in group.SectionGroups) {
  253.                 ForceGroupsRecursive(group.SectionGroups[sectionGroup.Name]);
  254.             }
  255.         }
  256.     }
  257. }

Developer Fusion