共用方式為


WebConfigurationManager.OpenMachineConfiguration 方法

定義

開啟電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。

多載

OpenMachineConfiguration()

開啟目前電腦上的電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。

OpenMachineConfiguration(String)

開啟目前電腦上的電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。

OpenMachineConfiguration(String, String)

開啟指定伺服器上的指定電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。

OpenMachineConfiguration(String, String, IntPtr)

使用指定的安全性內容,開啟指定伺服器上的指定電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。

OpenMachineConfiguration(String, String, String, String)

使用指定的安全性內容,開啟指定伺服器上的指定電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。

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

傳回

Configuration

Configuration 物件。

例外狀況

無法載入有效的組態檔。

範例

下列範例示範如何使用 方法來存取組態資訊 OpenMachineConfiguration


// Show how to use OpenMachineConfiguration().
// It gets the machine.config file on the current 
// machine and displays section information. 
static void OpenMachineConfiguration1()
{
    // Get the machine.config file on the current machine.
    System.Configuration.Configuration config =
            WebConfigurationManager.OpenMachineConfiguration();

    // Loop to get the sections. Display basic information.
    Console.WriteLine("Name, Allow Definition");
    int i = 0;
    foreach (ConfigurationSection section in config.Sections)
    {
        Console.WriteLine(
            section.SectionInformation.Name + "\t" +
        section.SectionInformation.AllowExeDefinition);
        i += 1;
    }
    Console.WriteLine("[Total number of sections: {0}]", i);

    // Display machine.config path.
    Console.WriteLine("[File path: {0}]", config.FilePath); 
}
' Show how to use OpenMachineConfiguration().
' It gets the machine.config file on the current 
' machine and displays section information. 
Shared Sub OpenMachineConfiguration1()
   ' Get the machine.config file on the current machine.
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenMachineConfiguration()
   
   ' Loop to get the sections. Display basic information.
   Console.WriteLine("Name, Allow Definition")
   Dim i As Integer = 0
   Dim section As ConfigurationSection
   For Each section In  config.Sections
         Console.WriteLine((section.SectionInformation.Name + _
         ControlChars.Tab + _
         section.SectionInformation.AllowExeDefinition.ToString()))
         i += 1
     Next section
     Console.WriteLine("[Total number of sections: {0}]", i)

     ' Display machine.config path.
     Console.WriteLine("[File path: {0}]", config.FilePath)
 End Sub

備註

方法 OpenMachineConfiguration 會在應用程式執行所在的電腦上開啟機器組態檔。 此檔案位於標準組建目錄 %windir%\Microsoft.NET\Framework\version\config。

另請參閱

適用於

OpenMachineConfiguration(String)

開啟目前電腦上的電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。

public:
 static System::Configuration::Configuration ^ OpenMachineConfiguration(System::String ^ locationSubPath);
public static System.Configuration.Configuration OpenMachineConfiguration (string locationSubPath);
static member OpenMachineConfiguration : string -> System.Configuration.Configuration
Public Shared Function OpenMachineConfiguration (locationSubPath As String) As Configuration

參數

locationSubPath
String

要套用電腦組態的應用程式路徑。

傳回

Configuration

Configuration 物件。

例外狀況

無法載入有效的組態檔。

範例

下列範例示範如何使用 方法來存取組態資訊 OpenMachineConfiguration


// Show how to use OpenMachineConfiguration(string).
// It gets the machine.config file applicabe to the
// specified resource and displays section 
// basic information. 
static void OpenMachineConfiguration2()
{
    // Get the machine.config file applicabe to the
    // specified reosurce.
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenMachineConfiguration("configTest");

    // Loop to get the sections. Display basic information.
    Console.WriteLine("Name, Allow Definition");
    int i = 0;
    foreach (ConfigurationSection section in config.Sections)
    {
        Console.WriteLine(
            section.SectionInformation.Name + "\t" +
        section.SectionInformation.AllowExeDefinition);
        i += 1;
    }
    Console.WriteLine("[Total number of sections: {0}]", i);

    // Display machine.config path.
    Console.WriteLine("[File path: {0}]", config.FilePath);
}
' Show how to use OpenMachineConfiguration(string).
' It gets the machine.config file applicabe to the
' specified resource and displays section 
' basic information. 
Shared Sub OpenMachineConfiguration2()
    ' Get the machine.config file applicabe to the
    ' specified reosurce.
    Dim config As System.Configuration.Configuration = _
    WebConfigurationManager.OpenMachineConfiguration( _
    "configTest")

    ' Loop to get the sections. Display basic information.
    Console.WriteLine("Name, Allow Definition")
    Dim i As Integer = 0
    Dim section As ConfigurationSection
    For Each section In config.Sections
        Console.WriteLine((section.SectionInformation.Name + _
        ControlChars.Tab + _
        section.SectionInformation.AllowExeDefinition.ToString()))
        i += 1
    Next section
    Console.WriteLine("[Total number of sections: {0}]", i)

    ' Display machine.config path.
    Console.WriteLine("[File path: {0}]", config.FilePath)
End Sub

備註

這個方法會開啟適用于 參數所指定目錄的 locationSubPath 機器組態檔。

另請參閱

適用於

OpenMachineConfiguration(String, String)

開啟指定伺服器上的指定電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。

public:
 static System::Configuration::Configuration ^ OpenMachineConfiguration(System::String ^ locationSubPath, System::String ^ server);
public static System.Configuration.Configuration OpenMachineConfiguration (string locationSubPath, string server);
static member OpenMachineConfiguration : string * string -> System.Configuration.Configuration
Public Shared Function OpenMachineConfiguration (locationSubPath As String, server As String) As Configuration

參數

locationSubPath
String

要套用此組態的應用程式路徑。

server
String

要傳回組態的完整伺服器名稱。

傳回

Configuration

Configuration 物件。

例外狀況

無法載入有效的組態檔。

範例

下列範例示範如何使用 方法來存取組態資訊 OpenMachineConfiguration


// Show how to use OpenMachineConfiguration(string, string).
// It gets the machine.config file on the specified server and
// applicabe to the specified reosurce and displays section 
// basic information. 
static void OpenMachineConfiguration3()
{
    // Get the machine.config file applicabe to the
    // specified reosurce and on the specified server.
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenMachineConfiguration("configTest", 
        "myServer");

    // Loop to get the sections. Display basic information.
    Console.WriteLine("Name, Allow Definition");
    int i = 0;
    foreach (ConfigurationSection section in config.Sections)
    {
        Console.WriteLine(
            section.SectionInformation.Name + "\t" +
        section.SectionInformation.AllowExeDefinition);
        i += 1;
    }
    Console.WriteLine("[Total number of sections: {0}]", i);

    // Display machine.config path.
    Console.WriteLine("[File path: {0}]", config.FilePath);
}
' Show how to use OpenMachineConfiguration(string, string).
' It gets the machine.config file on the specified server and
' applicabe to the specified reosurce and displays section 
' basic information. 
Shared Sub OpenMachineConfiguration3()
    ' Get the machine.config file applicabe to the
    ' specified reosurce and on the specified server.
    Dim config As System.Configuration.Configuration = _
    WebConfigurationManager.OpenMachineConfiguration( _
    "configTest", "myServer")

    ' Loop to get the sections. Display basic information.
    Console.WriteLine("Name, Allow Definition")
    Dim i As Integer = 0
    Dim section As ConfigurationSection
    For Each section In config.Sections
        Console.WriteLine((section.SectionInformation.Name + _
        ControlChars.Tab + _
        section.SectionInformation.AllowExeDefinition.ToString()))
        i += 1
    Next section
    Console.WriteLine("[Total number of sections: {0}]", i)

    ' Display machine.config path.
    Console.WriteLine("[File path: {0}]", config.FilePath)
End Sub

備註

這個方法會開啟位於 參數所指定 locationSubPath 之目錄中的機器組態檔,以及 參數所 server 指定的電腦上。

另請參閱

適用於

OpenMachineConfiguration(String, String, IntPtr)

使用指定的安全性內容,開啟指定伺服器上的指定電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。

public:
 static System::Configuration::Configuration ^ OpenMachineConfiguration(System::String ^ locationSubPath, System::String ^ server, IntPtr userToken);
public static System.Configuration.Configuration OpenMachineConfiguration (string locationSubPath, string server, IntPtr userToken);
static member OpenMachineConfiguration : string * string * nativeint -> System.Configuration.Configuration
Public Shared Function OpenMachineConfiguration (locationSubPath As String, server As String, userToken As IntPtr) As Configuration

參數

locationSubPath
String

要套用此組態的應用程式路徑。

server
String

要傳回組態的完整伺服器名稱。

userToken
IntPtr

nativeint

要使用的帳戶語彙基元。

傳回

Configuration

Configuration 物件。

例外狀況

沒有為 serveruserToken 參數提供有效值。

無法載入有效的組態檔。

範例

下列範例示範如何使用 方法來存取組態資訊 OpenMachineConfiguration


// Show how to use OpenMachineConfiguration(string, string).
// It gets the machine.config file on the specified server,
// applicabe to the specified reosurce, for the specified user
// and displays section basic information. 
static void OpenMachineConfiguration4()
{
    // Get the current user token.
    IntPtr userToken =
          System.Security.Principal.WindowsIdentity.GetCurrent().Token;

    // Get the machine.config file applicabe to the
    // specified reosurce, on the specified server for the
    // specified user.
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenMachineConfiguration("configTest",
        "myServer", userToken);

    // Loop to get the sections. Display basic information.
    Console.WriteLine("Name, Allow Definition");
    int i = 0;
    foreach (ConfigurationSection section in config.Sections)
    {
        Console.WriteLine(
            section.SectionInformation.Name + "\t" +
        section.SectionInformation.AllowExeDefinition);
        i += 1;
    }
    Console.WriteLine("[Total number of sections: {0}]", i);

    // Display machine.config path.
    Console.WriteLine("[File path: {0}]", config.FilePath);
}
' Show how to use OpenMachineConfiguration(string, string).
' It gets the machine.config file on the specified server,
' applicabe to the specified reosurce, for the specified user
' and displays section basic information. 
Shared Sub OpenMachineConfiguration4()
    ' Get the current user token.
    Dim userToken As IntPtr = _
    System.Security.Principal.WindowsIdentity.GetCurrent().Token

    ' Get the machine.config file applicabe to the
    ' specified reosurce, on the specified server for the
    ' specified user.
    Dim config As System.Configuration.Configuration = _
    WebConfigurationManager.OpenMachineConfiguration( _
    "configTest", "myServer", userToken)

    ' Loop to get the sections. Display basic information.
    Console.WriteLine("Name, Allow Definition")
    Dim i As Integer = 0
    Dim section As ConfigurationSection
    For Each section In config.Sections
        Console.WriteLine((section.SectionInformation.Name + _
        ControlChars.Tab + _
        section.SectionInformation.AllowExeDefinition.ToString()))
        i += 1
    Next section
    Console.WriteLine("[Total number of sections: {0}]", i)

    ' Display machine.config path.
    Console.WriteLine("[File path: {0}]", config.FilePath)
End Sub

備註

這個方法可用來使用模擬來存取組態檔。

注意

帳戶權杖通常是從 類別的 WindowsIdentity 實例擷取,或透過對 Unmanaged 程式碼的呼叫擷取,例如對 Windows API LogonUser 的呼叫。 如需對 Unmanaged 程式碼呼叫的詳細資訊,請參閱 取用 Unmanaged DLL 函式

另請參閱

適用於

OpenMachineConfiguration(String, String, String, String)

使用指定的安全性內容,開啟指定伺服器上的指定電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。

public:
 static System::Configuration::Configuration ^ OpenMachineConfiguration(System::String ^ locationSubPath, System::String ^ server, System::String ^ userName, System::String ^ password);
public static System.Configuration.Configuration OpenMachineConfiguration (string locationSubPath, string server, string userName, string password);
static member OpenMachineConfiguration : string * string * string * string -> System.Configuration.Configuration
Public Shared Function OpenMachineConfiguration (locationSubPath As String, server As String, userName As String, password As String) As Configuration

參數

locationSubPath
String

要套用此組態的應用程式路徑。

server
String

要傳回組態的完整伺服器名稱。

userName
String

開啟檔案時使用的完整使用者名稱 (Domain\User)。

password
String

使用者名稱的密碼。

傳回

Configuration

Configuration 物件。

例外狀況

serveruserNamepassword 參數無效。

無法載入有效的組態檔。

範例

下列範例示範如何使用 方法來存取組態資訊 OpenMachineConfiguration


// Show how to use OpenMachineConfiguration(string, string).
// It gets the machine.config file on the specified server,
// applicabe to the specified reosurce, for the specified user
// and displays section basic information. 
static void OpenMachineConfiguration5()
{
    // Set the user id and password.
    string user =
          System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    // Substitute with actual password.
    string password = "userPassword";

    // Get the machine.config file applicabe to the
    // specified reosurce, on the specified server for the
    // specified user.
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenMachineConfiguration("configTest",
        "myServer", user, password);

    // Loop to get the sections. Display basic information.
    Console.WriteLine("Name, Allow Definition");
    int i = 0;
    foreach (ConfigurationSection section in config.Sections)
    {
        Console.WriteLine(
            section.SectionInformation.Name + "\t" +
        section.SectionInformation.AllowExeDefinition);
        i += 1;
    }
    Console.WriteLine("[Total number of sections: {0}]", i);

    // Display machine.config path.
    Console.WriteLine("[File path: {0}]", config.FilePath);
}
 ' Show how to use OpenMachineConfiguration(string, string).
 ' It gets the machine.config file on the specified server,
 ' applicabe to the specified reosurce, for the specified user
 ' and displays section basic information. 
 Shared Sub OpenMachineConfiguration5()
     ' Set the user id and password.
     Dim user As String = _
     System.Security.Principal.WindowsIdentity.GetCurrent().Name
     ' Substitute with actual password.
     Dim password As String = "userPassword"

     ' Get the machine.config file applicabe to the
     ' specified reosurce, on the specified server for the
     ' specified user.
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenMachineConfiguration( _
     "configTest", "myServer", user, password)

     ' Loop to get the sections. Display basic information.
     Console.WriteLine("Name, Allow Definition")
     Dim i As Integer = 0
     Dim section As ConfigurationSection
     For Each section In config.Sections
         Console.WriteLine((section.SectionInformation.Name + _
         ControlChars.Tab + _
         section.SectionInformation.AllowExeDefinition.ToString()))
         i += 1
     Next section
     Console.WriteLine("[Total number of sections: {0}]", i)

     ' Display machine.config path.
     Console.WriteLine("[File path: {0}]", config.FilePath)
 End Sub

備註

這個方法可用來使用模擬來存取組態檔。

另請參閱

適用於