当前位置:网站首页>ConnectionOptions. Username attribute definition

ConnectionOptions. Username attribute definition

2022-06-21 14:55:00 allway2

Namespace :

System.Management

Assembly :

System.Management.dll

Gets or sets the user name that will be used for the connection operation .

C# Copy

public string Username { get; set; }

Property value

String

return  String  value , This value is used to connect to WMI User name used when .

Example

The following example connects to a remote computer , And displays information about the operating system on the remote computer . ConnectionOptions Create a connection to the remote computer using the required connection options .

C# Copy

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();
        options.Username = "UserName";

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

annotation

If the user name comes from a domain other than the current domain , The string may contain the domain name and user name , Separate with backslash : character string "username" = "EnterDomainHere \ \EnterUsernameHere". strUser Parameter cannot be an empty string .

Property value

null If the connection will use the currently logged in user , Then for ; otherwise , A string representing the user name .  The default value is  null.

.NET Framework Security

Full trust in the direct caller .  This member cannot be used by partially trusted code .  For more information , see also Using libraries from partially trusted code .

Apply to

Apply to
product edition
.NET Framework1.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
.NET Platform Extensions2.1, 2.2, 3.0, 3.1, 5.0, 6.0

原网站

版权声明
本文为[allway2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221333569228.html