ConnectionOptions.Username 属性定义

这段代码示例展示了如何在C#中使用System.Management命名空间连接到远程计算机,设置用户名并查询WMI(Windows Management Instrumentation)以获取操作系统信息。通过建立ConnectionOptions和ManagementScope,可以实现远程连接并执行查询。示例中,查询了远程计算机的计算机名、Windows目录、操作系统版本和制造商等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

命名空间:

System.Management

程序集:

System.Management.dll

获取或设置将用于连接操作的用户名。

C#复制

public string Username { get; set; }

属性值

String

返回 String 值,该值用作连接到 WMI 时使用的用户名。

示例

以下示例连接到远程计算机,并显示有关远程计算机上的操作系统的信息。 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();
        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"]);
        }
    }
}

注解

如果用户名来自当前域之外的域,则该字符串可能包含域名和用户名,用反斜杠分隔:字符串 "username" = "EnterDomainHere \ \EnterUsernameHere"。 strUser参数不能为空字符串。

属性值

null如果连接将使用当前登录的用户,则为;否则,表示用户名的字符串。 默认值为 null

.NET Framework 安全性

对直接调用方的完全信任。 此成员不能由部分信任的代码使用。 有关详细信息,请参阅从部分受信任的代码使用库

适用于

适用于
产品版本
.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值