MSI executing VB.net application, current user is returning the system user and not the logged on user

后端 未结 1 425
眼角桃花
眼角桃花 2021-01-23 16:09

My VB.net written application is being executed by an MSI file, and I need to get the currently logged on user (who is running the MSI). This is because I am importing xml files

1条回答
  •  迷失自我
    2021-01-23 16:48

    You could potentially try using WMI:

    Dim username, objItem
    Dim objWMIService  : Set objWMIService = GetObject( "winmgmts:\\.\root\cimv2" )
    Dim colItems  : Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem" )
    
    For Each objItem in colItems
        username = objItem.UserName
        if (instr(username ,"\") > 0) Then
            username = Split(username, "\")(1)
        end if   
    Next
    msgbox username
    

    Or Quser:

    Dim strCmd : strCmd = "cmd /q /c for /f ""skip=1 tokens=1"" %a in ('quser console') do @echo %a"
    username = CreateObject("WScript.Shell").Exec(strCmd).StdOut.ReadAll()
    username = Right(username,Len(username)-1)
    msgbox username
    

    0 讨论(0)
提交回复
热议问题