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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ConfigurationException.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.Globalization;
  19.     using System.IO;
  20.     using System.Runtime.Serialization;
  21.     using System.Security;
  22.     using System.Security.Permissions;
  23.     using System.Xml;
  24.     using System.Collections;
  25.     using System.Runtime.Versioning;
  26.    
  27.     // A config exception can contain a filename (of a config file)
  28.     // and a line number (of the location in the file in which a problem was
  29.     // encountered).
  30.     //
  31.     // Section handlers should throw this exception (or subclasses)
  32.     // together with filename and line nubmer information where possible.
  33.     [Serializable()]
  34.     public class ConfigurationException : SystemException
  35.     {
  36.         private const string HTTP_PREFIX = "http:";
  37.        
  38.         private string _filename;
  39.         private int _line;
  40.        
  41.         void Init(string filename, int line)
  42.         {
  43.             HResult = HResults.Configuration;
  44.             _filename = filename;
  45.             _line = line;
  46.         }
  47.        
  48.         // Default ctor is required for serialization.
  49.         protected ConfigurationException(SerializationInfo info, StreamingContext context) : base(info, context)
  50.         {
  51.             Init(info.GetString("filename"), info.GetInt32("line"));
  52.         }
  53.        
  54.         [Obsolete("This class is obsolete, to create a new exception create a System." + "Configuration!System.Configuration.ConfigurationErrorsException")]
  55.         public ConfigurationException() : this(null, null, null, 0)
  56.         {
  57.         }
  58.        
  59.         [Obsolete("This class is obsolete, to create a new exception create a System." + "Configuration!System.Configuration.ConfigurationErrorsException")]
  60.         public ConfigurationException(string message) : this(message, null, null, 0)
  61.         {
  62.         }
  63.        
  64.         [Obsolete("This class is obsolete, to create a new exception create a System." + "Configuration!System.Configuration.ConfigurationErrorsException")]
  65.         public ConfigurationException(string message, Exception inner) : this(message, inner, null, 0)
  66.         {
  67.         }
  68.        
  69.        
  70.         [Obsolete("This class is obsolete, to create a new exception create a System." + "Configuration!System.Configuration.ConfigurationErrorsException")]
  71.         public ConfigurationException(string message, XmlNode node) : this(message, null, GetUnsafeXmlNodeFilename(node), GetXmlNodeLineNumber(node))
  72.         {
  73.         }
  74.        
  75.         [Obsolete("This class is obsolete, to create a new exception create a System." + "Configuration!System.Configuration.ConfigurationErrorsException")]
  76.         public ConfigurationException(string message, Exception inner, XmlNode node) : this(message, inner, GetUnsafeXmlNodeFilename(node), GetXmlNodeLineNumber(node))
  77.         {
  78.         }
  79.        
  80.        
  81.         [Obsolete("This class is obsolete, to create a new exception create a System." + "Configuration!System.Configuration.ConfigurationErrorsException")]
  82.         public ConfigurationException(string message, string filename, int line) : this(message, null, filename, line)
  83.         {
  84.         }
  85.        
  86.         [Obsolete("This class is obsolete, to create a new exception create a System." + "Configuration!System.Configuration.ConfigurationErrorsException")]
  87.         public ConfigurationException(string message, Exception inner, string filename, int line) : base(message, inner)
  88.         {
  89.             Init(filename, line);
  90.         }
  91.        
  92.         [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
  93.         public override void GetObjectData(SerializationInfo info, StreamingContext context)
  94.         {
  95.             base.GetObjectData(info, context);
  96.             info.AddValue("filename", _filename);
  97.             info.AddValue("line", _line);
  98.         }
  99.        
  100.         // The message includes the file/line number information.
  101.         // To get the message without the extra information, use BareMessage.
  102.         public override string Message {
  103.             get {
  104.                 string file = Filename;
  105.                 if (!string.IsNullOrEmpty(file)) {
  106.                     if (Line != 0) {
  107.                         return BareMessage + " (" + file + " line " + Line.ToString(CultureInfo.InvariantCulture) + ")";
  108.                     }
  109.                     else {
  110.                         return BareMessage + " (" + file + ")";
  111.                     }
  112.                 }
  113.                 else if (Line != 0) {
  114.                     return BareMessage + " (line " + Line.ToString("G", CultureInfo.InvariantCulture) + ")";
  115.                 }
  116.                 else {
  117.                     return BareMessage;
  118.                 }
  119.             }
  120.         }
  121.        
  122.         public virtual string BareMessage {
  123.             get { return base.Message; }
  124.         }
  125.        
  126.         public virtual string Filename {
  127.             [ResourceExposure(ResourceScope.Machine)]
  128.             [ResourceConsumption(ResourceScope.Machine)]
  129.             get { return SafeFilename(_filename); }
  130.         }
  131.        
  132.         public virtual int Line {
  133.             get { return _line; }
  134.         }
  135.        
  136.         [Obsolete("This class is obsolete, use System.Configuration!System.Configuration." + "ConfigurationErrorsException.GetFilename instead")]
  137.         [ResourceExposure(ResourceScope.Machine)]
  138.         [ResourceConsumption(ResourceScope.Machine)]
  139.         public static string GetXmlNodeFilename(XmlNode node)
  140.         {
  141.             return SafeFilename(GetUnsafeXmlNodeFilename(node));
  142.         }
  143.        
  144.         [Obsolete("This class is obsolete, use System.Configuration!System.Configuration." + "ConfigurationErrorsException.GetLinenumber instead")]
  145.         public static int GetXmlNodeLineNumber(XmlNode node)
  146.         {
  147.             IConfigErrorInfo configNode = node as IConfigErrorInfo;
  148.            
  149.             if (configNode != null) {
  150.                 return configNode.LineNumber;
  151.             }
  152.             return 0;
  153.         }
  154.        
  155.         [FileIOPermission(SecurityAction.Assert, AllFiles = FileIOPermissionAccess.PathDiscovery)]
  156.         [ResourceExposure(ResourceScope.Machine)]
  157.         [ResourceConsumption(ResourceScope.Machine)]
  158.         private static string FullPathWithAssert(string filename)
  159.         {
  160.             string fullPath = null;
  161.            
  162.             try {
  163.                 fullPath = Path.GetFullPath(filename);
  164.             }
  165.             catch {
  166.             }
  167.            
  168.             return fullPath;
  169.         }
  170.        
  171.         //
  172.         // Internal Helper to strip a full path to just filename.ext when caller
  173.         // does not have path discovery to the path (used for sane error handling).
  174.         //
  175.         [ResourceExposure(ResourceScope.Machine)]
  176.         [ResourceConsumption(ResourceScope.Machine)]
  177.         static internal string SafeFilename(string filename)
  178.         {
  179.             if (string.IsNullOrEmpty(filename)) {
  180.                 return filename;
  181.             }
  182.            
  183.             // configuration file can be an http URL in IE
  184.             if (filename.StartsWith(HTTP_PREFIX, StringComparison.OrdinalIgnoreCase)) {
  185.                 return filename;
  186.             }
  187.            
  188.             try {
  189.                 // Confirm that it is a full path.
  190.                 // GetFullPath will also Demand PathDiscovery for the resulting path
  191.                 string fullPath = Path.GetFullPath(filename);
  192.             }
  193.             catch (SecurityException) {
  194.                 // Get just the name of the file without the directory part.
  195.                 try {
  196.                     string fullPath = FullPathWithAssert(filename);
  197.                     filename = Path.GetFileName(fullPath);
  198.                 }
  199.                 catch {
  200.                     filename = null;
  201.                 }
  202.             }
  203.             catch {
  204.                 filename = null;
  205.             }
  206.            
  207.             return filename;
  208.         }
  209.        
  210.         private static string GetUnsafeXmlNodeFilename(XmlNode node)
  211.         {
  212.             IConfigErrorInfo configNode = node as IConfigErrorInfo;
  213.            
  214.             if (configNode != null) {
  215.                 return configNode.Filename;
  216.             }
  217.            
  218.             return string.Empty;
  219.         }
  220.     }
  221. }

Developer Fusion