英語で読む

次の方法で共有


ConfigurationErrorsException クラス

定義

構成エラーが発生したときにスローされる例外。

C#
public class ConfigurationErrorsException : System.Configuration.ConfigurationException
C#
[System.Serializable]
public class ConfigurationErrorsException : System.Configuration.ConfigurationException
継承
属性

次のコード例では、カスタム セクションを作成し、そのプロパティを ConfigurationErrorsException 変更するときに例外を生成します。

C#
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();
        }
    }
}

次の例は、前の例で使用した構成の抜粋です。

XML
<?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 読み取りまたは書き込み中にエラーが発生すると、例外がスローされます。

コンストラクター

ConfigurationErrorsException()

ConfigurationErrorsException クラスの新しいインスタンスを初期化します。

ConfigurationErrorsException(SerializationInfo, StreamingContext)
古い.

ConfigurationErrorsException クラスの新しいインスタンスを初期化します。

ConfigurationErrorsException(String)

ConfigurationErrorsException クラスの新しいインスタンスを初期化します。

ConfigurationErrorsException(String, Exception)

ConfigurationErrorsException クラスの新しいインスタンスを初期化します。

ConfigurationErrorsException(String, Exception, String, Int32)

ConfigurationErrorsException クラスの新しいインスタンスを初期化します。

ConfigurationErrorsException(String, Exception, XmlNode)

ConfigurationErrorsException クラスの新しいインスタンスを初期化します。

ConfigurationErrorsException(String, Exception, XmlReader)

ConfigurationErrorsException クラスの新しいインスタンスを初期化します。

ConfigurationErrorsException(String, String, Int32)

ConfigurationErrorsException クラスの新しいインスタンスを初期化します。

ConfigurationErrorsException(String, XmlNode)

ConfigurationErrorsException クラスの新しいインスタンスを初期化します。

ConfigurationErrorsException(String, XmlReader)

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()

派生クラスでオーバーライドされた場合、それ以後に発生する 1 つ以上の例外の根本原因である 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)

適用対象

製品 バージョン
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9