ConfigurationManager.OpenMachineConfiguration 方法

定义

将当前计算机的配置文件作为 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 文件。

适用于

另请参阅