【问题标题】:How to get UserName from Computer?如何从计算机获取用户名?
【发布时间】:2015-12-15 11:13:31
【问题描述】:

我正在寻找找回红色块上的字符串。

我已经尝试过以下代码:

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

但是这段代码不起作用,因为我公司的电脑使用的是域,而我需要的String与用户不同。 用户仅由姓名组成,字符串由员工的姓名和名字组成。

【问题讨论】:

  • 不,这个只给我用户,我也试过这个,但是谢谢
  • Environment.GetEnvironmentVariable("%USERNAME%")怎么样
  • Environment.GetEnvironmentVariable("%USERNAME%") 给我一个空字符串

标签: windows vb.net active-directory windowsdomainaccount


【解决方案1】:

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

我们必须导入它:

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

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

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

【讨论】:

    【解决方案2】:

    您可以尝试使用任一

    HttpContext.Current.Server.MachineName
    

    System.Environment.MachineName;
    

    【讨论】:

    • 那给我计算机名称,我不需要它,我需要得到照片上的红色块。而且我认为是 %USERNAME% 之类的 Windows 变量,但我找不到它。
    • 从 ComputerName 中,您必须构建完整的路径。这就是它的工作方式。还是您要读取文件的路径?
    • 我不需要路径,我只需要红色块中的字符串。我必须打开一个像这个红色块一样调用的 excel 文件。
    • 这不是机器名吗?
    • 不,按下你的 Windows 按钮,点击用户,你会得到与照片相同的窗口。
    猜你喜欢
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    相关资源
    最近更新 更多