ConfigurationManager.OpenMappedExeConfiguration 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將指定的用戶端組態檔做為 Configuration 物件開啟。
多載
OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel) |
開啟指定的用戶端組態檔,做為使用指定檔案對應和使用者層級的 Configuration 物件。 |
OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean) |
開啟指定的用戶端組態檔,做為使用指定之檔案對應、使用者層級和預先載入選項的 Configuration 物件。 |
OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel)
開啟指定的用戶端組態檔,做為使用指定檔案對應和使用者層級的 Configuration 物件。
public:
static System::Configuration::Configuration ^ OpenMappedExeConfiguration(System::Configuration::ExeConfigurationFileMap ^ fileMap, System::Configuration::ConfigurationUserLevel userLevel);
public static System.Configuration.Configuration OpenMappedExeConfiguration (System.Configuration.ExeConfigurationFileMap fileMap, System.Configuration.ConfigurationUserLevel userLevel);
static member OpenMappedExeConfiguration : System.Configuration.ExeConfigurationFileMap * System.Configuration.ConfigurationUserLevel -> System.Configuration.Configuration
Public Shared Function OpenMappedExeConfiguration (fileMap As ExeConfigurationFileMap, userLevel As ConfigurationUserLevel) As Configuration
參數
- fileMap
- ExeConfigurationFileMap
要使用的組態檔,而非應用程式預設組態檔。
- userLevel
- ConfigurationUserLevel
其中一個列舉值,可指定您要開啟設定的使用者層級。
傳回
組態物件。
例外狀況
無法載入組態檔。
範例
下列程式代碼範例示範如何使用 OpenMappedExeConfiguration 方法來取得組態檔所包含的所有區段。
// 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);
}
' 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 Shared Sub MapExeConfiguration()
' Get the application configuration file.
Dim config As System.Configuration.Configuration = _
ConfigurationManager.OpenExeConfiguration( _
ConfigurationUserLevel.None)
Console.WriteLine(config.FilePath)
If config Is Nothing Then
Console.WriteLine( _
"The configuration file does not exist.")
Console.WriteLine( _
"Use OpenExeConfiguration to create file.")
End If
' Create a new configuration file by saving
' the application configuration to a new file.
Dim appName As String = _
Environment.GetCommandLineArgs()(0)
Dim configFile As String = _
String.Concat(appName, "2.config")
config.SaveAs(configFile, _
ConfigurationSaveMode.Full)
' Map the new configuration file.
Dim configFileMap As 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.
Dim sectionName As String = "consoleSection"
Dim customSection As ConsoleSection = _
DirectCast(config.GetSection(sectionName), _
ConsoleSection)
If customSection Is Nothing Then
customSection = New ConsoleSection()
config.Sections.Add(sectionName, customSection)
End If
' Change the section configuration values.
customSection = _
DirectCast(config.GetSection(sectionName), _
ConsoleSection)
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)
End Sub
備註
物件 ConfigurationUserLevel 會決定要開啟之組態檔的位置。 它指出檔案 (組態檔位於與應用程式) 相同的目錄中,或 (組態檔位於) 所 userLevel
決定的應用程式設定路徑中,則表示該檔案沒有用戶層級。
注意
若要取得 Configuration 資源的物件,您的程式代碼必須具有其繼承設定之所有組態檔的讀取許可權。 若要更新組態檔,您的程式代碼必須另外具有組態檔及其所在目錄的寫入許可權。
另請參閱
適用於
OpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean)
開啟指定的用戶端組態檔,做為使用指定之檔案對應、使用者層級和預先載入選項的 Configuration 物件。
public:
static System::Configuration::Configuration ^ OpenMappedExeConfiguration(System::Configuration::ExeConfigurationFileMap ^ fileMap, System::Configuration::ConfigurationUserLevel userLevel, bool preLoad);
public static System.Configuration.Configuration OpenMappedExeConfiguration (System.Configuration.ExeConfigurationFileMap fileMap, System.Configuration.ConfigurationUserLevel userLevel, bool preLoad);
static member OpenMappedExeConfiguration : System.Configuration.ExeConfigurationFileMap * System.Configuration.ConfigurationUserLevel * bool -> System.Configuration.Configuration
Public Shared Function OpenMappedExeConfiguration (fileMap As ExeConfigurationFileMap, userLevel As ConfigurationUserLevel, preLoad As Boolean) As Configuration
參數
- fileMap
- ExeConfigurationFileMap
要使用的組態檔,而非預設應用程式組態檔。
- userLevel
- ConfigurationUserLevel
其中一個列舉值,可指定您要開啟設定的使用者層級。
- preLoad
- Boolean
true
表示要預先載入所有區段群組和區段,否則為 false
。
傳回
組態物件。
例外狀況
無法載入組態檔。
備註
物件 ConfigurationUserLevel 會決定要開啟之組態檔的位置。 它指出檔案在與應用程式) 位於相同目錄中 (,或配置檔位於 (組態檔位於由) 所決定 userLevel
的應用程式設定路徑中,則表示該檔案沒有用戶層級。
注意
若要取得 Configuration 資源的物件,您的程式代碼必須具有其繼承設定之所有組態檔的讀取許可權。 若要更新組態檔,您的程式代碼必須另外具有組態檔及其所在目錄的寫入許可權。
如需程式代碼範例,請參閱 OpenMappedExeConfiguration 多載。