WebConfigurationManager.OpenMachineConfiguration 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將機器設定檔作為 Configuration 物件開啟,允許讀取或寫入操作。
多載
| 名稱 | Description |
|---|---|
| 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 物件。
例外狀況
無法載入有效的設定檔。
範例
以下範例說明如何利用該 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 物件。
例外狀況
無法載入有效的設定檔。
範例
以下範例說明如何利用該 OpenMachineConfiguration 方法存取設定資訊。
// Show how to use OpenMachineConfiguration(string).
// It gets the machine.config file applicable to the
// specified resource and displays section
// basic information.
static void OpenMachineConfiguration2()
{
// Get the machine.config file applicable to the
// specified resource.
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 物件。
例外狀況
無法載入有效的設定檔。
範例
以下範例說明如何利用該 OpenMachineConfiguration 方法存取設定資訊。
// Show how to use OpenMachineConfiguration(string, string).
// It gets the machine.config file on the specified server and
// applicable to the specified resource and displays section
// basic information.
static void OpenMachineConfiguration3()
{
// Get the machine.config file applicable to the
// specified resource 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 物件。
例外狀況
未提供 server OR userToken 參數的有效值。
無法載入有效的設定檔。
範例
以下範例說明如何利用該 OpenMachineConfiguration 方法存取設定資訊。
// Show how to use OpenMachineConfiguration(string, string).
// It gets the machine.config file on the specified server,
// applicable to the specified resource, 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 applicable to the
// specified resource, 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
備註
此方法用於透過模擬存取設定檔。
Note
帳號權杖通常從 WindowsIdentity 類別的實例中取得,或透過呼叫非管理程式碼,例如呼叫 Windows API LogonUser。 欲了解更多關於呼叫非受管理程式碼的資訊,請參閱 「消費非受管理 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 物件。
例外狀況
或與 serveruserNamepassword 參數均無效。
無法載入有效的設定檔。
範例
以下範例說明如何利用該 OpenMachineConfiguration 方法存取設定資訊。
// Show how to use OpenMachineConfiguration(string, string).
// It gets the machine.config file on the specified server,
// applicable to the specified resource, 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 applicable to the
// specified resource, 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
備註
此方法用於透過模擬存取設定檔。