ConfigurationErrorsException Klas
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
De uitzondering die wordt gegenereerd wanneer er een configuratiefout is opgetreden.
public ref class ConfigurationErrorsException : System::Configuration::ConfigurationException
[System.Serializable]
public class ConfigurationErrorsException : System.Configuration.ConfigurationException
[<System.Serializable>]
type ConfigurationErrorsException = class
inherit ConfigurationException
Public Class ConfigurationErrorsException
Inherits ConfigurationException
- Overname
- Kenmerken
Voorbeelden
In het volgende codevoorbeeld wordt een aangepaste sectie gemaakt en wordt er een ConfigurationErrorsException uitzondering gegenereerd wanneer de eigenschappen worden gewijzigd.
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
Het volgende voorbeeld is een configuratiefragment dat wordt gebruikt in het vorige voorbeeld.
<?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>
Opmerkingen
De ConfigurationErrorsException uitzondering wordt gegenereerd wanneer er een fout optreedt tijdens het lezen of schrijven van configuratiegegevens.
Constructors
Eigenschappen
| Name | Description |
|---|---|
| BareMessage |
Hiermee wordt een beschrijving opgehaald van de reden waarom deze configuratie-uitzondering is opgetreden. |
| Data |
Hiermee haalt u een verzameling sleutel-waardeparen op die aanvullende door de gebruiker gedefinieerde informatie over de uitzondering bieden. (Overgenomen van Exception) |
| Errors |
Hiermee wordt een verzameling fouten opgehaald die de redenen beschrijven waarom deze ConfigurationErrorsException uitzondering is opgetreden. |
| Filename |
Hiermee haalt u het pad op naar het configuratiebestand waardoor deze configuratie-uitzondering werd gegenereerd. |
| HelpLink |
Hiermee haalt u een koppeling op naar het Help-bestand dat aan deze uitzondering is gekoppeld. (Overgenomen van Exception) |
| HResult |
Hiermee wordt HRESULT opgehaald of ingesteld, een gecodeerde numerieke waarde die is toegewezen aan een specifieke uitzondering. (Overgenomen van Exception) |
| InnerException |
Hiermee haalt u het Exception exemplaar op dat de huidige uitzondering heeft veroorzaakt. (Overgenomen van Exception) |
| Line |
Hiermee wordt het regelnummer opgehaald in het configuratiebestand waarop deze configuratie-uitzondering is opgetreden. |
| Message |
Hiermee wordt een uitgebreide beschrijving opgehaald van de reden waarom deze configuratie-uitzondering is opgetreden. |
| Source |
Hiermee wordt de naam van de toepassing of het object dat de fout veroorzaakt, opgehaald of ingesteld. (Overgenomen van Exception) |
| StackTrace |
Hiermee haalt u een tekenreeksweergave van de directe frames op de aanroepstack op. (Overgenomen van Exception) |
| TargetSite |
Hiermee haalt u de methode op waarmee de huidige uitzondering wordt gegenereerd. (Overgenomen van Exception) |
Methoden
| Name | Description |
|---|---|
| Equals(Object) |
Bepaalt of het opgegeven object gelijk is aan het huidige object. (Overgenomen van Object) |
| GetBaseException() |
Wanneer deze wordt overschreven in een afgeleide klasse, retourneert u de Exception hoofdoorzaak van een of meer volgende uitzonderingen. (Overgenomen van Exception) |
| GetFilename(XmlNode) |
Hiermee wordt het pad naar het configuratiebestand opgehaald waaruit het interne XmlNode object is geladen toen deze configuratie-uitzondering werd gegenereerd. |
| GetFilename(XmlReader) |
Hiermee wordt het pad naar het configuratiebestand opgehaald dat het interne XmlReader bestand heeft gelezen toen deze configuratie-uitzondering werd gegenereerd. |
| GetHashCode() |
Fungeert als de standaardhashfunctie. (Overgenomen van Object) |
| GetLineNumber(XmlNode) |
Hiermee haalt u het regelnummer op in het configuratiebestand dat het interne XmlNode object weergeeft wanneer deze configuratie-uitzondering is opgetreden. |
| GetLineNumber(XmlReader) |
Hiermee wordt het regelnummer opgehaald in het configuratiebestand dat het interne XmlReader object heeft verwerkt toen deze configuratie-uitzondering werd gegenereerd. |
| GetObjectData(SerializationInfo, StreamingContext) |
Hiermee stelt u het SerializationInfo object in met de bestandsnaam en het regelnummer waarop deze configuratie-uitzondering heeft plaatsgevonden. |
| GetType() |
Hiermee haalt u het runtimetype van het huidige exemplaar op. (Overgenomen van Exception) |
| MemberwiseClone() |
Hiermee maakt u een ondiepe kopie van de huidige Object. (Overgenomen van Object) |
| ToString() |
Hiermee maakt en retourneert u een tekenreeksweergave van de huidige uitzondering. (Overgenomen van Exception) |
gebeurtenis
| Name | Description |
|---|---|
| SerializeObjectState |
Treedt op wanneer een uitzondering wordt geserialiseerd om een uitzonderingsstatusobject te maken dat geserialiseerde gegevens over de uitzondering bevat. (Overgenomen van Exception) |