ConfigurationManager クラス
アセンブリ: System.Configuration (system.configuration.dll 内)

Public NotInheritable Class ConfigurationManager

ConfigurationManager クラスを使用すると、マシン構成情報、アプリケーション構成情報、およびユーザー構成情報にアクセスできます。このクラスは ConfigurationSettings クラスに代わるもので、優先して使用されます。
クライアント アプリケーションはクライアント コンピュータで実行され、Web アプリケーションはクライアントの接続先であるサーバー コンピュータで実行されます。
System.Configuration の組み込みの型またはこれらの型から派生した型を使用して、構成情報を処理できます。これらの型を使用すると、構成情報を直接処理することが可能で、構成ファイルを拡張してカスタム情報を含めることもできます。
ConfigurationManager クラスには、次のタスクを実行できるメンバが含まれています。
-
構成ファイルからセクションを読み取ります。構成情報にアクセスするには、GetSection メソッドを呼び出します。appSettings や connectionStrings などの一部のセクションには、AppSettings クラスおよび ConnectionStrings クラスを使用します。これらのメンバは、読み取り専用の操作を実行し、構成のキャッシュされた 1 つのインスタンスを使用し、マルチスレッド対応です。
-
構成ファイル全体の読み取りと書き込みを行います。アプリケーションは、そのアプリケーション自体や他のアプリケーション、またはコンピュータのあらゆるレベルで、ローカルまたはリモートから構成設定の読み取りと書き込みを行うことができます。ConfigurationManager クラスに用意されているメソッドのいずれかを使用して、SampleApp.exe.config などの構成ファイルを開くことができます。これらのメソッドは、Configuration オブジェクトを返します。このオブジェクトは、関連付けられている構成ファイルの処理に使用できるメソッドとプロパティを公開します。メソッドは、読み取り操作または書き込み操作を実行し、ファイルに書き込みが行われるたびに構成データを再作成します。
-
構成タスクをサポートします。次の型を使用して、さまざまな構成タスクをサポートします。
-
SectionInformation
-
PropertyInformation
-
PropertyInformationCollection
-
ElementInformation
-
ContextInformation
-
ConfigurationSectionGroup
-
ConfigurationSectionGroupCollection
既存の構成情報の処理に加え、ConfigurationElement、ConfigurationElementCollection、ConfigurationProperty、ConfigurationSection の各クラスなど、組み込みの構成の種類を拡張することによって、カスタム構成要素を作成し使用できます。組み込みの構成の種類をプログラムによって拡張する方法の例については、ConfigurationSection に関するトピックを参照してください。属性ベースのモデルを使用して、組み込みの構成の種類を拡張する方法の例については、ConfigurationElement に関するトピックを参照してください。
-

ConfigurationManager クラスを使用して、カスタム構成セクションにアクセスする方法を次のコード例に示します。セクションが存在しない場合は、セクションが作成され、構成ファイルに追加されます。この例で使用するカスタム セクション クラスは、ConfigurationPropertyAttribute クラスに含まれる例で実装されます。
' Create a custom section. Shared Sub New() ' Get the application configuration file. Dim config As System.Configuration.Configuration = _ ConfigurationManager.OpenExeConfiguration( _ ConfigurationUserLevel.None) ' Console.WriteLine(config.FilePath); ' If the section does not exiat in the configuration ' file, create it and save it to the file. If config.Sections(customSectionName) Is Nothing Then custSection = New CustomSection() config.Sections.Add(customSectionName, custSection) custSection = config.GetSection(customSectionName) custSection.SectionInformation.ForceSave = True config.Save(ConfigurationSaveMode.Full) End If End Sub 'New
// Create a custom section. static UsingConfigurationManager() { // 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[customSectionName] == null) { custSection = new CustomSection(); config.Sections.Add(customSectionName, custSection); custSection = config.GetSection(customSectionName) as CustomSection; custSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } }
この例では、次の構成ファイルに示される要素を処理します。この構成ファイルは、このコード例を初めて実行したときに生成されます。
![]() |
---|
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="CustomSection" type="Samples.Config.CustomSection, ConfigurationManager, Version=1.0.0.0, Culture=neutralPublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" /> </configSections> <connectionStrings> <add name="ConnStr1" connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb" providerName="System.Data.SqlClient" /> <add name="ConnStr2" connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings> <add key="AppSetting0" value="Monday, February 28, 2005 1:27:59 PM" /> <add key="AppSetting1" value="Monday, February 28, 2005 1:36:40 PM" /> </appSettings> <CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:10:00" /> </configuration>

System.Configuration.ConfigurationManager


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ConfigurationManager メンバ
System.Configuration 名前空間
Configuration クラス
WebConfigurationManager
その他の技術情報
アプリケーションの設定
Weblioに収録されているすべての辞書からConfigurationManager クラスを検索する場合は、下記のリンクをクリックしてください。

- ConfigurationManager クラスのページへのリンク