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

蓝咒 提交于 2019-12-02 14:13:32

问题


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 into the task scheduler and without the correct usersname, there is a mapping error. Currently, because the application is being run through the MSI or windows installer the System user is being used. This is causing a mapping error so I was wondering if there is any other way to find the logged on user.

MsgBox(Environment.UserName)

Dim WSHNetwork = CreateObject("WScript.Network")
MsgBox(WSHNetwork.Username)

Both message boxes return "SYSTEM", whereas I need it to return the actual logged on user.


回答1:


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


来源:https://stackoverflow.com/questions/56623998/msi-executing-vb-net-application-current-user-is-returning-the-system-user-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!