ConfigurationManager.OpenMachineConfiguration 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將目前電腦上的電腦組態檔開啟做為 Configuration 物件。
public:
static System::Configuration::Configuration ^ OpenMachineConfiguration();
public static System.Configuration.Configuration OpenMachineConfiguration ();
static member OpenMachineConfiguration : unit -> System.Configuration.Configuration
Public Shared Function OpenMachineConfiguration () As Configuration
傳回
電腦組態檔。
例外狀況
無法載入組態檔。
範例
下列程式代碼範例示範如何使用 OpenMachineConfiguration 方法來取得組態檔中包含的所有區段。
// Access the machine configuration file using mapping.
// The function uses the OpenMappedMachineConfiguration
// method to access the machine configuration.
public static void MapMachineConfiguration()
{
// Get the machine.config file.
Configuration machineConfig =
ConfigurationManager.OpenMachineConfiguration();
// Get the machine.config file path.
ConfigurationFileMap configFile =
new ConfigurationFileMap(machineConfig.FilePath);
// Map the application configuration file to the machine
// configuration file.
Configuration config =
ConfigurationManager.OpenMappedMachineConfiguration(
configFile);
// Get the AppSettings section.
AppSettingsSection appSettingSection =
(AppSettingsSection)config.GetSection("appSettings");
appSettingSection.SectionInformation.AllowExeDefinition =
ConfigurationAllowExeDefinition.MachineToRoamingUser;
// Display the configuration file sections.
ConfigurationSectionCollection sections =
config.Sections;
Console.WriteLine();
Console.WriteLine("Using OpenMappedMachineConfiguration.");
Console.WriteLine("Sections in machine.config:");
// Get the sections in the machine.config.
foreach (ConfigurationSection section in sections)
{
string name = section.SectionInformation.Name;
Console.WriteLine("Name: {0}", name);
}
}
' Access the machine configuration file using mapping.
' The function uses the OpenMappedMachineConfiguration
' method to access the machine configuration.
Public Shared Sub MapMachineConfiguration()
' Get the machine.config file.
Dim machineConfig As Configuration = _
ConfigurationManager.OpenMachineConfiguration()
' Get the machine.config file path.
Dim configFile _
As New ConfigurationFileMap( _
machineConfig.FilePath)
' Map the application configuration file
' to the machine configuration file.
Dim config As Configuration = _
ConfigurationManager. _
OpenMappedMachineConfiguration( _
configFile)
' Get the AppSettings section.
Dim appSettingSection As AppSettingsSection = _
DirectCast(config.GetSection("appSettings"), _
AppSettingsSection)
appSettingSection.SectionInformation. _
AllowExeDefinition = _
ConfigurationAllowExeDefinition. _
MachineToRoamingUser
' Display the configuration file sections.
Dim sections As _
ConfigurationSectionCollection = _
config.Sections
Console.WriteLine()
Console.WriteLine( _
"Using OpenMappedMachineConfiguration.")
Console.WriteLine( _
"Sections in machine.config:")
' Get the sections in the machine.config.
For Each section _
As ConfigurationSection In sections
Dim name As String = _
section.SectionInformation.Name
Console.WriteLine("Name: {0}", name)
Next
End Sub
備註
除非針對本機應用程式覆寫,否則計算機組態設定會套用至整個計算機及其上的所有應用程式。 計算機組態設定是從目前執行 .NET Framework 版本的 Machine.config 檔案讀取。 Machine.config 檔案位於下列子目錄中:
%windir%\Microsoft.NET\Framework\version\config
注意
若要取得 Configuration 資源的物件,您的程式代碼必須具有其繼承設定之所有組態檔的讀取許可權。 若要更新組態檔,您的程式代碼必須另外具有組態檔及其所在目錄的寫入許可權。 無法存取電腦上可能安裝之其他 .NET Framework 版本的 Machine.config 檔案。