ConfigurationManager.OpenMachineConfiguration ConfigurationManager.OpenMachineConfiguration ConfigurationManager.OpenMachineConfiguration ConfigurationManager.OpenMachineConfiguration Method

定义

将当前计算机上的计算机配置文件作为 Configuration 对象打开。Opens the machine configuration file on the current computer as a Configuration object.

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方法获取配置文件中包含的所有节。The following code example shows how to use the OpenMachineConfiguration method to obtain all sections that are contained in the configuration file.

   // 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

注解

除非为本地应用程序重写,否则计算机配置设置将应用于整个计算机以及驻留在该计算机上的所有应用程序。Machine configuration settings apply to the whole computer and all applications that reside on it, unless overridden for the local application. 计算机配置设置是从 .NET Framework 当前运行版本的 machine.config 文件中读取的。Machine configuration settings are read from the Machine.config file of the currently running version of the .NET Framework. Machine.config 文件位于以下子目录中:The Machine.config file is located in the following subdirectory:

%windir% \Microsoft.NET\Framework\version\config%windir% \Microsoft.NET\Framework\version\config

备注

若要获取Configuration资源的对象,你的代码必须对它继承其设置的所有配置文件具有读取权限。To obtain the Configuration object for a resource, your code must have read permissions on all the configuration files from which it inherits settings. 若要更新配置文件,你的代码还必须对配置文件及其所在的目录具有写入权限。To update a configuration file, your code must additionally have write permissions for both the configuration file and the directory in which it exists. 对于可能安装在计算机上的其他版本的 .NET Framework,不能访问 machine.config 文件。It is not possible to access the Machine.config file for other versions of the .NET Framework that might be installed on the computer.

适用于

另请参阅