代码之家  ›  专栏  ›  技术社区  ›  GMX

在vb 2013中获取当前的windows配置文件

  •  0
  • GMX  · 技术社区  · 10 年前

    我正在编写一个小vb应用程序。 我需要知道登录的Windows用户名帐户。 我发现了这个:

    Function GetUserName() As String
        If TypeOf My.User.CurrentPrincipal Is 
          Security.Principal.WindowsPrincipal Then
            ' The application is using Windows authentication.
            ' The name format is DOMAIN\USERNAME.
            Dim parts() As String = Split(My.User.Name, "\")
            Dim username As String = parts(1)
            Return username
        Else
            ' The application is using custom authentication.
            Return My.User.Name
        End If
    End Function
    

    工作正常,但他给了我执行流程的用户,因此如果我使用管理凭据启动应用程序,此功能将给我管理用户。 我需要Windows用户,而不是执行进程的用户,我该怎么做?

    谢谢

    3 回复  |  直到 10 年前
        1
  •  0
  •   Creator    10 年前

    这将为您提供登录到windows的用户的用户名。“不是启动程序的人”

    您需要为此导入系统管理。

    Imports System.Management
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        Dim Coll As ManagementObjectCollection
        Dim LogonName As String
        Dim GetName As New ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem")
        Coll = GetName.[Get]()
        LogonName = DirectCast(Coll.Cast(Of ManagementBaseObject)().First()("UserName"), String)
        Dim CleanName() As String = Split(LogonName, "\")
        Label1.Text = CleanName(1)
    
    End Sub
    
        2
  •  0
  •   ohSkittle    10 年前
    Environment.UserName
    

    返回正在运行程序的用户

    Environment.UserDomainName
    

    返回计算机的名称

    我希望我回答了你的问题:)

        3
  •  0
  •   GMX    10 年前

    好的,我添加到我的参考系统。管理和现在工作得很好。谢谢