如何从计算机获取用户名?

How to get UserName from Computer?

我正在搜索以取回红色块上的字符串。

我已经尝试过以下代码:

Environment.UserName
My.User.Name
SystemInformation.UserName
System.Security.Principal.WindowsIdentity.GetCurrent().Name
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Environment.GetFolderPath(Environment.GetEnvironmentVariable("%USERNAME%"))

但是这段代码不行,因为我公司的电脑用的是域,我需要的String跟用户不一样。 用户只由姓名组成,字符串由姓名和员工名组成。

您可以尝试使用其中一种

HttpContext.Current.Server.MachineName

System.Environment.MachineName;

我终于找到了我需要的东西here

我们必须导入:

“System.DirectoryServices.AccountManagement”
“System.DirectoryServices”

并写下这两行以取回红色块。

Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current