WebConfigurationManager クラス
アセンブリ: System.Web (system.web.dll 内)


WebConfigurationManager を使用すると、マシン情報やアプリケーション情報にアクセスできます。
Web アプリケーション関連の構成ファイルを処理する場合は、WebConfigurationManager の使用をお勧めします。クライアント アプリケーションの場合は、ConfigurationManager を使用します。
次に説明するように、作成したアプリケーションで System.Configuration 型を拡張したり、それを直接使用して構成情報を処理したりできます。
-
構成の処理。標準型を使用して構成情報を処理するには、次のいずれかの方法を使用します。
-
セクションへのアクセス。アプリケーションの構成情報にアクセスするには、WebConfigurationManager によって提供された GetSection メソッドのいずれかを使用する必要があります。appSettings と connectionStrings の場合は、AppSettings プロパティと ConnectionStrings プロパティを使用します。これらのメソッドは、読み取り専用の操作を実行し、構成のキャッシュされた 1 つのインスタンスを使用し、マルチスレッド対応です。
-
構成ファイルへのアクセス。アプリケーションは、そのアプリケーション自体や他のアプリケーション、またはコンピュータのあらゆるレベルで、ローカルまたはリモートから構成設定の読み取りと書き込みを行うことができます。WebConfigurationManager によって提供された open メソッドの 1 つを使用します。これらのメソッドは、Configuration オブジェクトを返します。このオブジェクトにより、基になる構成ファイルを処理するために必要なメソッドとプロパティが提供されます。これらのメソッドは、読み取り操作または書き込み操作を実行し、ファイルが開かれるたびに構成データを再作成します。
-
詳細な構成。より詳細な構成処理は、SectionInformation、PropertyInformation、PropertyInformationCollection、ElementInformation、ContextInformation、ConfigurationSectionGroup、ConfigurationSectionGroupCollection、および WebConfigurationSectionGroup の各型によって提供されます。
-
-
構成の標準型の拡張。ConfigurationElement、ConfigurationElementCollection、ConfigurationProperty、および ConfigurationSection などの標準の構成を、プログラムや属性モデルを使用して拡張して、カスタム構成要素を提供することもできます。構成の標準型をプログラムを使用して拡張する方法の例については、ConfigurationSection のトピックを参照してください。構成の標準型を属性モデルを使用して拡張する方法の例については、ConfigurationElement のトピックを参照してください。
![]() |
---|
パス パラメータを受け取る静的な GetSection メソッドを使用する場合、パス パラメータは、コードを実行しているアプリケーションを参照している必要があります。そうでない場合は、パラメータが無視され、現在実行中のアプリケーションの構成情報が返されます。 |

WebConfigurationManager メソッドを使用して構成情報にアクセスする方法を次のコード例に示します。
' Show the use of GetSection(string). ' It gets the connectiobStrings section. ' If called from within a client application, ' the GetSection(string) gets the default connectionStrings ' section from the machine.config. ' If called from within a Web aplication it gets the ' section from the configuration file located at the ' application current level. Shared Sub GetSection1() ' Get the connectionStrings section. Dim connectionStringsSection As ConnectionStringsSection = _ WebConfigurationManager.GetSection("connectionStrings") ' Get the connectionStrings key,value pairs collection. Dim connectionStrings As ConnectionStringSettingsCollection = _ connectionStringsSection.ConnectionStrings ' Get the collection enumerator. Dim connectionStringsEnum As IEnumerator = _ connectionStrings.GetEnumerator() ' Loop through the collection and ' display the connectionStrings key, value pairs. Dim i As Integer = 0 Console.WriteLine("[Display the connectionStrings]") While connectionStringsEnum.MoveNext() Dim name As String = connectionStrings(i).Name Console.WriteLine("Name: {0} Value: {1}", _ name, connectionStrings(name)) i += 1 End While Console.WriteLine() End Sub 'GetSection1
// Show the use of GetSection(string). // It gets the connectiobStrings section. // If called from within a client application, // the GetSection(string) gets the default connectionStrings // section from the machine.config. // If called from within a Web aplication it gets the // section from the configuration file located at the // application current level. static void GetSection1() { // Get the connectionStrings section. ConnectionStringsSection connectionStringsSection = WebConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection; // Get the connectionStrings key,value pairs collection. ConnectionStringSettingsCollection connectionStrings = connectionStringsSection.ConnectionStrings; // Get the collection enumerator. IEnumerator connectionStringsEnum = connectionStrings.GetEnumerator(); // Loop through the collection and // display the connectionStrings key, value pairs. int i = 0; Console.WriteLine("[Display the connectionStrings]"); while (connectionStringsEnum.MoveNext()) { string name = connectionStrings[i].Name; Console.WriteLine("Name: {0} Value: {1}", name, connectionStrings[name]); i += 1; } Console.WriteLine(); }

System.Web.Configuration.WebConfigurationManager


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


WebConfigurationManager メンバ
System.Web.Configuration 名前空間
Configuration
ConfigurationManager
その他の技術情報
ASP.NET 構成の概要
- WebConfigurationManager クラスのページへのリンク