ConnectionOptions 建構函式

定義

建立新的 ConnectionOption。

多載

ConnectionOptions()

來源:
ManagementOptions.cs
來源:
ManagementOptions.cs
來源:
ManagementOptions.cs

使用預設值,為連接作業初始化 ConnectionOptions 類別的新執行個體。 這是無參數建構函式。

C#
public ConnectionOptions ();

範例

下列範例會連線到遠端電腦,並顯示遠端電腦上作業系統的相關信息。 會建立 , ConnectionOptions 以使用預設連線選項連線到遠端電腦。

C#
using System;
using System.Management;
public class RemoteConnect
{
    public static void Main()
    {
        // Build an options object for the remote connection
        // if you plan to connect to the remote
        // computer with a different user name
        // and password than the one you are currently using.
        // This example uses the default values.

        ConnectionOptions options =
            new ConnectionOptions();

        // Make a connection to a remote computer.
        // Replace the "FullComputerName" section of the
        // string "\\\\FullComputerName\\root\\cimv2" with
        // the full computer name or IP address of the
        // remote computer.
        ManagementScope scope =
            new ManagementScope(
            "\\\\FullComputerName\\root\\cimv2", options);
        scope.Connect();

        //Query system for Operating System information
        ObjectQuery query = new ObjectQuery(
            "SELECT * FROM Win32_OperatingSystem");
        ManagementObjectSearcher searcher =
            new ManagementObjectSearcher(scope,query);

        ManagementObjectCollection queryCollection = searcher.Get();
        foreach ( ManagementObject m in queryCollection)
        {
            // Display the remote computer information
            Console.WriteLine("Computer Name : {0}",
                m["csname"]);
            Console.WriteLine("Windows Directory : {0}",
                m["WindowsDirectory"]);
            Console.WriteLine("Operating System: {0}",
                m["Caption"]);
            Console.WriteLine("Version: {0}", m["Version"]);
            Console.WriteLine("Manufacturer : {0}",
                m["Manufacturer"]);
        }
    }
}

備註

.NET Framework 安全性

完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫

適用於

.NET Framework 4.8.1 和其他版本
產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

ConnectionOptions(String, String, SecureString, String, ImpersonationLevel, AuthenticationLevel, Boolean, ManagementNamedValueCollection, TimeSpan)

來源:
ManagementOptions.cs
來源:
ManagementOptions.cs
來源:
ManagementOptions.cs

建立新的 ConnectionOption。

C#
public ConnectionOptions (string locale, string username, System.Security.SecureString password, string authority, System.Management.ImpersonationLevel impersonation, System.Management.AuthenticationLevel authentication, bool enablePrivileges, System.Management.ManagementNamedValueCollection context, TimeSpan timeout);

參數

locale
String

要用於連接的地區設定 (Locale)。

username
String

要用於連接的使用者名稱。 如果是 Null,則會使用目前登入使用者的認證。

password
SecureString

指定使用者名稱的密碼。 如果使用者名稱也是 Null,則使用的認證將是目前登入使用者的認證。

authority
String

要用來驗證指定使用者的授權。

impersonation
ImpersonationLevel

要用於連接的 COM 模擬等級。

authentication
AuthenticationLevel

要用於連接的 COM 驗證 (Authentication) 等級。

enablePrivileges
Boolean

true 表示啟用特殊的使用者權限,否則為 false。 在執行需要特殊 Windows NT 使用者權限的作業時才會使用這個參數。

context
ManagementNamedValueCollection

要經由提供者傳送的特定提供者 (Provider) 具名值配對物件。

timeout
TimeSpan

保留供未來使用。

適用於

.NET Framework 4.8.1 和其他版本
產品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

ConnectionOptions(String, String, String, String, ImpersonationLevel, AuthenticationLevel, Boolean, ManagementNamedValueCollection, TimeSpan)

來源:
ManagementOptions.cs
來源:
ManagementOptions.cs
來源:
ManagementOptions.cs

使用指定值,初始化要用於 WMI 連接的 ConnectionOptions 類別的新執行個體。

C#
public ConnectionOptions (string locale, string username, string password, string authority, System.Management.ImpersonationLevel impersonation, System.Management.AuthenticationLevel authentication, bool enablePrivileges, System.Management.ManagementNamedValueCollection context, TimeSpan timeout);

參數

locale
String

要用於連接的地區設定 (Locale)。

username
String

要用於連接的使用者名稱。 如果是 Null,則會使用目前登入使用者的認證。

password
String

指定使用者名稱的密碼。 如果使用者名稱也是 Null,則使用的認證將是目前登入使用者的認證。

authority
String

要用來驗證指定使用者的授權。

impersonation
ImpersonationLevel

要用於連接的 COM 模擬等級。

authentication
AuthenticationLevel

要用於連接的 COM 驗證 (Authentication) 等級。

enablePrivileges
Boolean

若要啟用特殊的使用者權限,應為 true,否則為 false。 在執行需要特殊 Windows NT 使用者權限的作業時才會使用這個參數。

context
ManagementNamedValueCollection

要經由提供者傳送的特定提供者 (Provider) 具名值配對物件。

timeout
TimeSpan

保留供未來使用。

範例

下列範例會連線到遠端電腦,並顯示遠端電腦上作業系統的相關信息。 會建立 , ConnectionOptions 以使用所需的連線選項連線到遠端電腦。

C#
using System;
using System.Management;
using System.Security;

public class RemoteConnect
{
    public static void Main()
    {
        // Build an options object for the remote connection
        // if you plan to connect to the remote
        // computer with a different user name
        // and password than the one you are currently using.
        SecureString pw = GetPassword();
        ConnectionOptions options =
            new ConnectionOptions("MS_409", "userName", pw,
            "ntlmdomain:DOMAIN",
            System.Management.ImpersonationLevel.Impersonate,
            System.Management.AuthenticationLevel.Default, true,
            null, System.TimeSpan.MaxValue);
        pw.Dispose();

        // Make a connection to a remote computer.
        // Replace the "FullComputerName" section of the
        // string "\\\\FullComputerName\\root\\cimv2" with
        // the full computer name or IP address of the
        // remote computer.
        ManagementScope scope =
            new ManagementScope(
            "\\\\FullComputerName\\root\\cimv2", options);
        scope.Connect();

        //Query system for Operating System information
        ObjectQuery query = new ObjectQuery(
            "SELECT * FROM Win32_OperatingSystem");
        ManagementObjectSearcher searcher =
            new ManagementObjectSearcher(scope, query);

        ManagementObjectCollection queryCollection = searcher.Get();
        foreach (ManagementObject m in queryCollection)
        {
            // Display the remote computer information
            Console.WriteLine("Computer Name : {0}",
                m["csname"]);
            Console.WriteLine("Windows Directory : {0}",
                m["WindowsDirectory"]);
            Console.WriteLine("Operating System: {0}",
                m["Caption"]);
            Console.WriteLine("Version: {0}", m["Version"]);
            Console.WriteLine("Manufacturer : {0}",
                m["Manufacturer"]);
        }
    }

    /// <summary>
    /// Read a password from the console into a SecureString
    /// </summary>
    /// <returns>Password stored in a secure string</returns>
    public static SecureString GetPassword()
    {
        SecureString password = new SecureString();
        Console.WriteLine("Enter password: ");

        // get the first character of the password
        ConsoleKeyInfo nextKey = Console.ReadKey(true);

        while (nextKey.Key != ConsoleKey.Enter)
        {
            if (nextKey.Key == ConsoleKey.Backspace)
            {
                if (password.Length > 0)
                {
                    password.RemoveAt(password.Length - 1);

                    // erase the last * as well
                    Console.Write(nextKey.KeyChar);
                    Console.Write(" ");
                    Console.Write(nextKey.KeyChar);
                }
            }
            else
            {
                password.AppendChar(nextKey.KeyChar);
                Console.Write("*");
            }

            nextKey = Console.ReadKey(true);
        }

        Console.WriteLine();

        // lock the password down
        password.MakeReadOnly();
        return password;
    }
}

備註

.NET Framework 安全性

完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫

適用於

.NET Framework 4.8.1 和其他版本
產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1