ConfigurationManager.OpenMachineConfiguration Metodo

Definizione

Apre il file di configurazione del computer corrente come oggetto 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

Restituisce

Configuration

Oggetto Configuration .A Configuration object.

Eccezioni

Non è stato possibile caricare un file di configurazione.A configuration file could not be loaded.

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare il metodo OpenMachineConfiguration per ottenere tutte le sezioni contenute nel file di configurazione.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

Commenti

Le impostazioni di configurazione del computer si applicano all'intero computer e a tutte le applicazioni che vi risiedono, a meno che non vengano sostituite per l'applicazione locale.Machine configuration settings apply to the whole computer and all applications that reside on it, unless overridden for the local application. Le impostazioni di configurazione del computer vengono lette dal file Machine. config della versione attualmente in esecuzione del .NET Framework.Machine configuration settings are read from the Machine.config file of the currently running version of the .NET Framework. Il file Machine. config si trova nella sottodirectory seguente:The Machine.config file is located in the following subdirectory:

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

Nota

Per ottenere l'oggetto Configuration per una risorsa, è necessario che il codice disponga delle autorizzazioni di lettura per tutti i file di configurazione da cui eredita le impostazioni.To obtain the Configuration object for a resource, your code must have read permissions on all the configuration files from which it inherits settings. Per aggiornare un file di configurazione, il codice deve disporre anche delle autorizzazioni di scrittura per il file di configurazione e la directory in cui è presente.To update a configuration file, your code must additionally have write permissions for both the configuration file and the directory in which it exists. Non è possibile accedere al file Machine. config per le altre versioni del .NET Framework che potrebbero essere installate nel computer.It is not possible to access the Machine.config file for other versions of the .NET Framework that might be installed on the computer.

Si applica a

Vedi anche