命名空间:
程序集:
System.Management.dll
获取或设置将用于连接操作的用户名。
C#复制
public string Username { get; set; }
属性值
返回 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 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 |
.NET Platform Extensions | 2.1, 2.2, 3.0, 3.1, 5.0, 6.0 |