ConfigurationErrorsException 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
发生配置错误时引发的异常。
public ref class ConfigurationErrorsException : System::Configuration::ConfigurationExceptionpublic class ConfigurationErrorsException : System.Configuration.ConfigurationException[System.Serializable]
public class ConfigurationErrorsException : System.Configuration.ConfigurationExceptiontype ConfigurationErrorsException = class
    inherit ConfigurationException[<System.Serializable>]
type ConfigurationErrorsException = class
    inherit ConfigurationExceptionPublic Class ConfigurationErrorsException
Inherits ConfigurationException- 继承
- 属性
示例
下面的代码示例创建自定义节,并在修改其属性时生成 ConfigurationErrorsException 异常。
using System;
using System.Configuration;
using System.Collections.Specialized;
using System.Collections;
namespace Samples.AspNet
{
    // Define a custom section.
    public sealed class CustomSection :
       ConfigurationSection
    {
        public CustomSection()
        {
        }
        [ConfigurationProperty("fileName", DefaultValue = "default.txt",
                    IsRequired = true, IsKey = false)]
        [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\",
            MinLength = 1, MaxLength = 60)]
        public string FileName
        {
            get
            {
                return (string)this["fileName"];
            }
            set
            {
                this["fileName"] = value;
            }
        }
        [ConfigurationProperty("maxUsers", DefaultValue = (long)10,
            IsRequired = false)]
        [LongValidator(MinValue = 1, MaxValue = 100,
            ExcludeRange = false)]
        public long MaxUsers
        {
            get
            {
                return (long)this["maxUsers"];
            }
            set
            {
                this["maxUsers"] = value;
            }
        }
    }
    // Create the custom section and write it to
    // the configuration file.
    class UsingConfigurationErrorsException
    {
        // Create a custom section.
        static UsingConfigurationErrorsException()
        {
            // Get the application configuration file.
            System.Configuration.Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None);
            // If the section does not exist in the configuration
            // file, create it and save it to the file.
            if (config.Sections["CustomSection"] == null)
            {
                CustomSection custSection = new CustomSection();
                config.Sections.Add("CustomSection", custSection);
                custSection =
                    config.GetSection("CustomSection") as CustomSection;
                custSection.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Full);
            }
        }
        
        // Modify a custom section and cause configuration 
        // error exceptions.
        static void ModifyCustomSection()
        {
            try
            {
                // Get the application configuration file.
                System.Configuration.Configuration config =
                        ConfigurationManager.OpenExeConfiguration(
                        ConfigurationUserLevel.None);
                CustomSection custSection =
                   config.Sections["CustomSection"] as CustomSection;
                // Change the section properties.
                custSection.FileName = "newName.txt";
                
                // Cause an exception.
                custSection.MaxUsers = custSection.MaxUsers + 100;
                if (!custSection.ElementInformation.IsLocked)
                    config.Save();
                else
                    Console.WriteLine(
                        "Section was locked, could not update.");
            }
            catch (ConfigurationErrorsException err)
            {
                string msg = err.Message;
                Console.WriteLine("Message: {0}", msg);
                string fileName = err.Filename;
                Console.WriteLine("Filename: {0}", fileName);
                int lineNumber = err.Line;
                Console.WriteLine("Line: {0}", lineNumber.ToString());
                string bmsg = err.BareMessage;
                Console.WriteLine("BareMessage: {0}", bmsg);
                string source = err.Source;
                Console.WriteLine("Source: {0}", source);
                string st = err.StackTrace;
                Console.WriteLine("StackTrace: {0}", st);
            }
        }
        static void Main(string[] args)
        {
            ModifyCustomSection();
        }
    }
}
Imports System.Configuration
Imports System.Collections.Specialized
Imports System.Collections
' Define a custom section.
NotInheritable Public Class CustomSection
    Inherits ConfigurationSection
    
    Public Sub New() 
    
    End Sub
    
    
    <ConfigurationProperty("fileName", DefaultValue:="default.txt", IsRequired:=True, IsKey:=False), StringValidator(InvalidCharacters:=" ~!@#$%^&*()[]{}/;'""|\", MinLength:=1, MaxLength:=60)> _
    Public Property FileName() As String
        Get
            Return CStr(Me("fileName"))
        End Get
        Set(ByVal value As String)
            Me("fileName") = value
        End Set
    End Property
    
    
    <ConfigurationProperty("maxUsers", DefaultValue:=10, IsRequired:=False), LongValidator(MinValue:=1, MaxValue:=100, ExcludeRange:=False)> _
    Public Property MaxUsers() As Long
        Get
            Return Fix(Me("maxUsers"))
        End Get
        Set(ByVal value As Long)
            Me("maxUsers") = value
        End Set
    End Property
End Class
' Create the custom section and write it to
' the configuration file.
Class UsingConfigurationErrorsException
    
    ' Create a custom section.
    Shared Sub New()
        ' Get the application configuration file.
        Dim config _
        As System.Configuration.Configuration = _
        ConfigurationManager.OpenExeConfiguration( _
        ConfigurationUserLevel.None)
        ' If the section does not exist in the configuration
        ' file, create it and save it to the file.
        If config.Sections("CustomSection") Is Nothing Then
            Dim custSection As New CustomSection()
            config.Sections.Add("CustomSection", custSection)
            custSection = config.GetSection("CustomSection")
            custSection.SectionInformation.ForceSave = True
            config.Save(ConfigurationSaveMode.Full)
        End If
    End Sub
    
    
    ' Modify a custom section and cause configuration 
    ' error exceptions.
    Shared Sub ModifyCustomSection() 
        
        Try
            ' Get the application configuration file.
            Dim config _
            As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)
            Dim custSection _
            As CustomSection = _
            config.Sections("CustomSection")
             
            ' Change the section properties.
            custSection.FileName = "newName.txt"
            
            ' Cause an exception.
            custSection.MaxUsers = _
            custSection.MaxUsers + 100
            
            If Not custSection.ElementInformation.IsLocked Then
                config.Save()
            Else
                Console.WriteLine( _
                "Section was locked, could not update.")
            End If
        Catch err As ConfigurationErrorsException
            
            Dim msg As String = err.Message
            Console.WriteLine("Message: {0}", msg)
            Dim fileName As String = err.Filename
            Console.WriteLine("Filename: {0}", _
            fileName)
            Dim lineNumber As Integer = err.Line
            Console.WriteLine("Line: {0}", _
            lineNumber.ToString())
            Dim bmsg As String = err.BareMessage
            Console.WriteLine("BareMessage: {0}", bmsg)
            Dim src As String = err.Source
            Console.WriteLine("Source: {0}", src)
            Dim st As String = err.StackTrace
            Console.WriteLine("StackTrace: {0}", st)
        End Try
    End Sub
    Shared Sub Main(ByVal args() As String) 
        ModifyCustomSection()
    
    End Sub
End Class
以下示例是上一个示例使用的配置摘录。
<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <configSections>  
    <section name="CustomSection" type="Samples.AspNet.CustomSection,   
      ConfigurationErrorsException, Version=1.0.0.0, Culture=neutral,   
      PublicKeyToken=null" allowDefinition="Everywhere"   
      allowExeDefinition="MachineToApplication"   
      restartOnExternalChanges="true" />  
  </configSections>  
  <CustomSection fileName="default.txt" maxUsers="10" />  
</configuration>  
注解
读取 ConfigurationErrorsException 或写入配置信息时发生任何错误时,将引发异常。
构造函数
属性
| BareMessage | 获取对引发此配置异常的原因的说明。 | 
| BareMessage | 获取对引发此配置异常的原因的说明。(继承自 ConfigurationException) | 
| Data | 获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。(继承自 Exception) | 
| Errors | 获取详细描述引发此 ConfigurationErrorsException 异常的原因的错误集合。 | 
| Filename | 获取导致引发此配置异常的配置文件的路径。 | 
| HelpLink | 获取或设置指向与此异常关联的帮助文件链接。(继承自 Exception) | 
| HResult | 获取或设置 HRESULT(一个分配给特定异常的编码数字值)。(继承自 Exception) | 
| InnerException | 获取导致当前异常的 Exception 实例。(继承自 Exception) | 
| Line | 获取配置文件中引发此配置异常处的行号。 | 
| Message | 获取对引发此配置异常的原因的补充说明。 | 
| Source | 获取或设置导致错误的应用程序或对象的名称。(继承自 Exception) | 
| StackTrace | 获取调用堆栈上的即时框架字符串表示形式。(继承自 Exception) | 
| TargetSite | 获取引发当前异常的方法。(继承自 Exception) | 
方法
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetBaseException() | 当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。(继承自 Exception) | 
| GetFilename(XmlNode) | 获取引发此配置异常时从中加载内部 XmlNode 对象的配置文件的路径。 | 
| GetFilename(XmlReader) | 获取引发此配置异常时内部 XmlReader 正在读取的配置文件的路径。 | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetLineNumber(XmlNode) | 获取引发此配置异常时内部 XmlNode 对象所表示的配置文件中的行号。 | 
| GetLineNumber(XmlReader) | 获取引发此配置异常时内部 XmlReader 对象正在处理的配置文件中的行号。 | 
| GetObjectData(SerializationInfo, StreamingContext) | 
				已过时.
			 用发生此配置异常处的文件名和行号设置 SerializationInfo 对象。 | 
| GetType() | 获取当前实例的运行时类型。(继承自 Exception) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 创建并返回当前异常的字符串表示形式。(继承自 Exception) | 
事件
| SerializeObjectState | 
		已过时.
	 当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。(继承自 Exception) |