ConnectionOptions Constructors

Definition

Creates a new ConnectionOption.

Overloads

ConnectionOptions()

Initializes a new instance of the ConnectionOptions class for the connection operation, using default values. This is the parameterless constructor.

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

Creates a new ConnectionOption.

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

Initializes a new instance of the ConnectionOptions class to be used for a WMI connection, using the specified values.

ConnectionOptions()

Source:
ManagementOptions.cs
Source:
ManagementOptions.cs
Source:
ManagementOptions.cs

Initializes a new instance of the ConnectionOptions class for the connection operation, using default values. This is the parameterless constructor.

public ConnectionOptions ();

Examples

The following example connects to a remote computer and displays information about the operating system on the remote computer. A ConnectionOptions is created to connect to the remote computer with default connection options.

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"]);
        }
    }
}

Remarks

.NET Framework Security

Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.

Applies to

.NET 9 (package-provided) 和其他版本
产品 版本
.NET 8 (package-provided), 9 (package-provided)
.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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

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

Source:
ManagementOptions.cs
Source:
ManagementOptions.cs
Source:
ManagementOptions.cs

Creates a new ConnectionOption.

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);

Parameters

locale
String

The locale to be used for the connection.

username
String

The user name to be used for the connection. If null, the credentials of the currently logged-on user are used.

password
SecureString

The password for the given user name. If the user name is also null, the credentials used will be those of the currently logged-on user.

authority
String

The authority to be used to authenticate the specified user.

impersonation
ImpersonationLevel

The COM impersonation level to be used for the connection.

authentication
AuthenticationLevel

The COM authentication level to be used for the connection.

enablePrivileges
Boolean

true to enable special user privileges; otherwise, false. This parameter should only be used when performing an operation that requires special Windows NT user privileges.

context
ManagementNamedValueCollection

A provider-specific, named value pairs object to be passed through to the provider.

timeout
TimeSpan

Reserved for future use.

Applies to

.NET 9 (package-provided) 和其他版本
产品 版本
.NET 8 (package-provided), 9 (package-provided)
.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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

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

Source:
ManagementOptions.cs
Source:
ManagementOptions.cs
Source:
ManagementOptions.cs

Initializes a new instance of the ConnectionOptions class to be used for a WMI connection, using the specified values.

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);

Parameters

locale
String

The locale to be used for the connection.

username
String

The user name to be used for the connection. If null, the credentials of the currently logged-on user are used.

password
String

The password for the given user name. If the user name is also null, the credentials used will be those of the currently logged-on user.

authority
String

The authority to be used to authenticate the specified user.

impersonation
ImpersonationLevel

The COM impersonation level to be used for the connection.

authentication
AuthenticationLevel

The COM authentication level to be used for the connection.

enablePrivileges
Boolean

true to enable special user privileges; otherwise, false. This parameter should only be used when performing an operation that requires special Windows NT user privileges.

context
ManagementNamedValueCollection

A provider-specific, named value pairs object to be passed through to the provider.

timeout
TimeSpan

Reserved for future use.

Examples

The following example connects to a remote computer and displays information about the operating system on the remote computer. A ConnectionOptions is created to connect to the remote computer with the desired connection options.

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;
    }
}

Remarks

.NET Framework Security

Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.

Applies to

.NET 9 (package-provided) 和其他版本
产品 版本
.NET 8 (package-provided), 9 (package-provided)
.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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)