mscorlib(4.0.0.0) API with additions
ConfigurationException.cs
3 using System.IO;
5 using System.Security;
7 using System.Xml;
8 
9 namespace System.Configuration
10 {
12  [Serializable]
14  {
15  private const string HTTP_PREFIX = "http:";
16 
17  private string _filename;
18 
19  private int _line;
20 
23  public override string Message
24  {
25  get
26  {
27  string filename = Filename;
28  if (!string.IsNullOrEmpty(filename))
29  {
30  if (Line != 0)
31  {
32  return BareMessage + " (" + filename + " line " + Line.ToString(CultureInfo.InvariantCulture) + ")";
33  }
34  return BareMessage + " (" + filename + ")";
35  }
36  if (Line != 0)
37  {
38  return BareMessage + " (line " + Line.ToString("G", CultureInfo.InvariantCulture) + ")";
39  }
40  return BareMessage;
41  }
42  }
43 
46  public virtual string BareMessage => base.Message;
47 
50  public virtual string Filename => SafeFilename(_filename);
51 
54  public virtual int Line => _line;
55 
56  private void Init(string filename, int line)
57  {
58  base.HResult = -2146232062;
59  _filename = filename;
60  _line = line;
61  }
62 
67  : base(info, context)
68  {
69  Init(info.GetString("filename"), info.GetInt32("line"));
70  }
71 
73  [Obsolete("This class is obsolete, to create a new exception create a System.Configuration!System.Configuration.ConfigurationErrorsException")]
75  : this(null, null, null, 0)
76  {
77  }
78 
81  [Obsolete("This class is obsolete, to create a new exception create a System.Configuration!System.Configuration.ConfigurationErrorsException")]
82  public ConfigurationException(string message)
83  : this(message, null, null, 0)
84  {
85  }
86 
90  [Obsolete("This class is obsolete, to create a new exception create a System.Configuration!System.Configuration.ConfigurationErrorsException")]
91  public ConfigurationException(string message, Exception inner)
92  : this(message, inner, null, 0)
93  {
94  }
95 
99  [Obsolete("This class is obsolete, to create a new exception create a System.Configuration!System.Configuration.ConfigurationErrorsException")]
100  public ConfigurationException(string message, XmlNode node)
101  : this(message, null, GetUnsafeXmlNodeFilename(node), GetXmlNodeLineNumber(node))
102  {
103  }
104 
109  [Obsolete("This class is obsolete, to create a new exception create a System.Configuration!System.Configuration.ConfigurationErrorsException")]
110  public ConfigurationException(string message, Exception inner, XmlNode node)
111  : this(message, inner, GetUnsafeXmlNodeFilename(node), GetXmlNodeLineNumber(node))
112  {
113  }
114 
119  [Obsolete("This class is obsolete, to create a new exception create a System.Configuration!System.Configuration.ConfigurationErrorsException")]
120  public ConfigurationException(string message, string filename, int line)
121  : this(message, null, filename, line)
122  {
123  }
124 
130  [Obsolete("This class is obsolete, to create a new exception create a System.Configuration!System.Configuration.ConfigurationErrorsException")]
131  public ConfigurationException(string message, Exception inner, string filename, int line)
132  : base(message, inner)
133  {
134  Init(filename, line);
135  }
136 
140  [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
141  public override void GetObjectData(SerializationInfo info, StreamingContext context)
142  {
143  base.GetObjectData(info, context);
144  info.AddValue("filename", _filename);
145  info.AddValue("line", _line);
146  }
147 
151  [Obsolete("This class is obsolete, use System.Configuration!System.Configuration.ConfigurationErrorsException.GetFilename instead")]
152  public static string GetXmlNodeFilename(XmlNode node)
153  {
154  return SafeFilename(GetUnsafeXmlNodeFilename(node));
155  }
156 
160  [Obsolete("This class is obsolete, use System.Configuration!System.Configuration.ConfigurationErrorsException.GetLinenumber instead")]
161  public static int GetXmlNodeLineNumber(XmlNode node)
162  {
163  return (node as IConfigErrorInfo)?.LineNumber ?? 0;
164  }
165 
166  [FileIOPermission(SecurityAction.Assert, AllFiles = FileIOPermissionAccess.PathDiscovery)]
167  private static string FullPathWithAssert(string filename)
168  {
169  string result = null;
170  try
171  {
172  result = Path.GetFullPath(filename);
173  return result;
174  }
175  catch
176  {
177  return result;
178  }
179  }
180 
181  internal static string SafeFilename(string filename)
182  {
183  if (string.IsNullOrEmpty(filename))
184  {
185  return filename;
186  }
187  if (filename.StartsWith("http:", StringComparison.OrdinalIgnoreCase))
188  {
189  return filename;
190  }
191  try
192  {
193  if (!Path.IsPathRooted(filename))
194  {
195  return filename;
196  }
197  }
198  catch
199  {
200  return null;
201  }
202  try
203  {
204  string fullPath = Path.GetFullPath(filename);
205  return filename;
206  }
207  catch (SecurityException)
208  {
209  try
210  {
211  string path = FullPathWithAssert(filename);
212  filename = Path.GetFileName(path);
213  return filename;
214  }
215  catch
216  {
217  filename = null;
218  return filename;
219  }
220  }
221  catch
222  {
223  filename = null;
224  return filename;
225  }
226  }
227 
228  private static string GetUnsafeXmlNodeFilename(XmlNode node)
229  {
230  IConfigErrorInfo configErrorInfo = node as IConfigErrorInfo;
231  if (configErrorInfo != null)
232  {
233  return configErrorInfo.Filename;
234  }
235  return string.Empty;
236  }
237  }
238 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
Describes a set of security permissions applied to code. This class cannot be inherited.
FileIOPermissionAccess
Specifies the type of file access requested.
ConfigurationException(string message, XmlNode node)
Initializes a new instance of the T:System.Configuration.ConfigurationException class.
Serves as the base class for system exceptions namespace.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
ConfigurationException(string message, Exception inner)
Initializes a new instance of the T:System.Configuration.ConfigurationException class.
Definition: __Canon.cs:3
static string GetFileName(string path)
Returns the file name and extension of the specified path string.
Definition: Path.cs:914
Describes the source and destination of a given serialized stream, and provides an additional caller-...
ConfigurationException(string message, Exception inner, XmlNode node)
Initializes a new instance of the T:System.Configuration.ConfigurationException class.
virtual int Line
Gets the line number within the configuration file at which this configuration exception was thrown.
SecurityAction
Specifies the security actions that can be performed using declarative security.
virtual string BareMessage
Gets a description of why this configuration exception was thrown.
static int GetXmlNodeLineNumber(XmlNode node)
Gets the line number within the configuration file that the internal T:System.Xml....
static string GetXmlNodeFilename(XmlNode node)
Gets the path to the configuration file from which the internal T:System.Xml.XmlNode object was loade...
ConfigurationException(string message)
Initializes a new instance of the T:System.Configuration.ConfigurationException class.
override void GetObjectData(SerializationInfo info, StreamingContext context)
Sets the T:System.Runtime.Serialization.SerializationInfo object with the file name and line number a...
ConfigurationException(string message, string filename, int line)
Initializes a new instance of the T:System.Configuration.ConfigurationException class.
static string GetFullPath(string path)
Returns the absolute path for the specified path string.
Definition: Path.cs:446
override string Message
Gets an extended description of why this configuration exception was thrown.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
The exception that is thrown when a configuration system error has occurred.
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
Specifies that the class can be serialized.
ConfigurationException()
Initializes a new instance of the T:System.Configuration.ConfigurationException class.
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Controls the ability to access files and folders. This class cannot be inherited.
ConfigurationException(string message, Exception inner, string filename, int line)
Initializes a new instance of the T:System.Configuration.ConfigurationException class.
ConfigurationException(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.Configuration.ConfigurationException class.
static bool IsPathRooted(string path)
Gets a value indicating whether the specified path string contains a root.
Definition: Path.cs:1084
The exception that is thrown when a security error is detected.
Represents a single node in the XML document.
Definition: XmlNode.cs:13
virtual string Filename
Gets the path to the configuration file that caused this configuration exception to be thrown.
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13