ConfigurationManager.OpenMappedExeConfiguration メソッド

定義

指定したクライアント構成ファイルを Configuration オブジェクトとして開きます。

オーバーロード

OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel)

指定したクライアント構成ファイルを、指定したファイル マップとユーザー レベルを使用する Configuration オブジェクトとして開きます。

OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean)

指定したクライアント構成ファイルを、指定したファイル マップ、ユーザー レベル、およびプリロード オプションを使用する Configuration オブジェクトとして開きます。

OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel)

ソース:
ConfigurationManager.cs
ソース:
ConfigurationManager.cs
ソース:
ConfigurationManager.cs

指定したクライアント構成ファイルを、指定したファイル マップとユーザー レベルを使用する Configuration オブジェクトとして開きます。

C#
public static System.Configuration.Configuration OpenMappedExeConfiguration (System.Configuration.ExeConfigurationFileMap fileMap, System.Configuration.ConfigurationUserLevel userLevel);

パラメーター

fileMap
ExeConfigurationFileMap

アプリケーションの既定の構成ファイルの代わりに使用する構成ファイル。

userLevel
ConfigurationUserLevel

構成を開く対象のユーザー レベルを指定する列挙値の 1 つ。

戻り値

構成オブジェクト。

例外

構成ファイルを読み込めませんでした。

次のコード例は、 メソッドを使用して、 OpenMappedExeConfiguration 構成ファイルに含まれるすべてのセクションを取得する方法を示しています。

C#

   // Access a configuration file using mapping.
   // This function uses the OpenMappedExeConfiguration 
   // method to access a new configuration file.   
   // It also gets the custom ConsoleSection and 
   // sets its ConsoleElement BackgroundColor and
   // ForegroundColor properties to green and red
   // respectively. Then it uses these properties to
   // set the console colors.  
   public static void MapExeConfiguration()
   {

     // Get the application configuration file.
     System.Configuration.Configuration config =
       ConfigurationManager.OpenExeConfiguration(
             ConfigurationUserLevel.None);
   
     Console.WriteLine(config.FilePath);

     if (config == null)
     {
       Console.WriteLine(
         "The configuration file does not exist.");
       Console.WriteLine(
        "Use OpenExeConfiguration to create the file.");
     }

     // Create a new configuration file by saving 
     // the application configuration to a new file.
     string appName = 
       Environment.GetCommandLineArgs()[0];

     string configFile =  string.Concat(appName, 
       ".2.config");
     config.SaveAs(configFile, ConfigurationSaveMode.Full);

     // Map the new configuration file.
     ExeConfigurationFileMap configFileMap = 
         new ExeConfigurationFileMap();
     configFileMap.ExeConfigFilename = configFile;

     // Get the mapped configuration file
    config = 
       ConfigurationManager.OpenMappedExeConfiguration(
         configFileMap, ConfigurationUserLevel.None);

     // Make changes to the new configuration file. 
     // This is to show that this file is the 
     // one that is used.
     string sectionName = "consoleSection";

     ConsoleSection customSection =
       (ConsoleSection)config.GetSection(sectionName);

     if (customSection == null)
     {
         customSection = new ConsoleSection();
         config.Sections.Add(sectionName, customSection);
     }
     else
         // Change the section configuration values.
         customSection =
             (ConsoleSection)config.GetSection(sectionName);

     customSection.ConsoleElement.BackgroundColor =
         ConsoleColor.Green;
     customSection.ConsoleElement.ForegroundColor =
         ConsoleColor.Red;

     // Save the configuration file.
     config.Save(ConfigurationSaveMode.Modified);

     // Force a reload of the changed section. This 
     // makes the new values available for reading.
     ConfigurationManager.RefreshSection(sectionName);

     // Set console properties using the 
     // configuration values contained in the 
     // new configuration file.
     Console.BackgroundColor =
       customSection.ConsoleElement.BackgroundColor;
     Console.ForegroundColor =
       customSection.ConsoleElement.ForegroundColor;
     Console.Clear();

     Console.WriteLine();
     Console.WriteLine("Using OpenMappedExeConfiguration.");
     Console.WriteLine("Configuration file is: {0}", 
       config.FilePath);
   }

注釈

オブジェクトは ConfigurationUserLevel 、開いている構成ファイルの場所を決定します。 これは、ファイルにユーザー レベルがない (構成ファイルがアプリケーションと同じディレクトリにある) か、ユーザーごとのレベル (構成ファイルが によって userLevel決定されたアプリケーション設定パス内にある) かどうかを示します。

注意

リソースの オブジェクトを取得するには、設定を Configuration 継承するすべての構成ファイルに対する読み取りアクセス許可がコードに必要です。 構成ファイルを更新するには、構成ファイルとそれが存在するディレクトリの両方に対する書き込みアクセス許可をコードに追加する必要があります。

こちらもご覧ください

適用対象

.NET Framework 4.8.1 およびその他のバージョン
製品 バージョン
.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

OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean)

ソース:
ConfigurationManager.cs
ソース:
ConfigurationManager.cs
ソース:
ConfigurationManager.cs

指定したクライアント構成ファイルを、指定したファイル マップ、ユーザー レベル、およびプリロード オプションを使用する Configuration オブジェクトとして開きます。

C#
public static System.Configuration.Configuration OpenMappedExeConfiguration (System.Configuration.ExeConfigurationFileMap fileMap, System.Configuration.ConfigurationUserLevel userLevel, bool preLoad);

パラメーター

fileMap
ExeConfigurationFileMap

アプリケーションの既定の構成ファイルの代わりに使用する構成ファイル。

userLevel
ConfigurationUserLevel

構成を開く対象のユーザー レベルを指定する列挙値の 1 つ。

preLoad
Boolean

すべてのセクション グループとセクションをプリロードする場合は true。それ以外の場合は false

戻り値

構成オブジェクト。

例外

構成ファイルを読み込めませんでした。

注釈

オブジェクトは ConfigurationUserLevel 、開いている構成ファイルの場所を決定します。 これは、ファイルにユーザー レベルがない (構成ファイルがアプリケーションと同じディレクトリにある) か、ユーザー単位のレベル (構成ファイルは によって userLevel決定されるアプリケーション設定パス内にある) かどうかを示します。

注意

リソースの オブジェクトを取得するには、設定を Configuration 継承するすべての構成ファイルに対する読み取りアクセス許可がコードに必要です。 構成ファイルを更新するには、構成ファイルとそれが存在するディレクトリの両方に対する書き込みアクセス許可をコードに追加する必要があります。

コード例については、オーバーロードに関するページを OpenMappedExeConfiguration 参照してください。

適用対象

.NET Framework 4.8.1 およびその他のバージョン
製品 バージョン
.NET Framework 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