Return Windows logonid with shellexecute


Daun

Recommended Posts

I am trying to capture the windows user currently logged in using the shellexecute()

private string user =null
   ? user
user = system.ShellExecute("cmd.exe" ,"open","/k whoami")
   ? user

As long as there is no error in my syntax, it always returns 42, if there is an error that the cmd doesn't like it returns 2.

I assume my failure is setting the variable to the "system.shellexecute".

How do I set user to the username from the cmd.exe or even directly from whoami.exe?

:rolleyes:

Thanks

Link to comment
Share on other sites

You don't want /k, otherwise the cmd window will remain open. In fact you probably want /c.

Shell commands can only return a status from the shell. They don't return the output of a command. That goes to stdout. You'd have to somehow pipe the result of whoami to a file and then use the file. functions to read it. Probably the easiest way to do this (because I'm not sure you can pipe directly from shellExecute) would be to create a batch file. So, shellExecute would look something like this:

system.ShellExecute("cmd.exe", "open", "\whoAmIBatch.bat")

then whoAmIBatch.bat would contain something like this:

whoami > whoami.txt

then use file. functions to read whoami.txt.

Note you might be able to do shellexecute directly on the bat file, but I don't think so.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.