Getting the wrong process output - System.Diagnostics.Process

  • Thread starter Thread starter thereisnopatchforhumancruelty
  • Start date Start date
T

thereisnopatchforhumancruelty

Guest
Hi, I'm implementing a Windows service using C# which launch the whoami command to retrieve the user logged on the machine.


Process process = new Process();
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = @"/C whoami";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
string hostAndUser = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
//do something with the string hostAndUser
//...
//...





The process starts with a specific user which is the admin of the machine in which the service is executing (in the myservice Log-On section of services.msc there is .\myuser).

But the output is: mymachine\mymachine instead of mymachine\myuser (myuser is the currently logged user on mymachine).

Why I'm getting the wrong whoami output?

Thank you.

Continue reading...
 
Back
Top