I am making an application which allows a client to list all their running processes in a dialog box. I currently have the below code, and I can't work out why it isn't working.
I am not seeing any output whatsoever, be it sderr or stdout. Can someone please point me in the right direction?
private void button1_Click(object sender, EventArgs e)
{
string test = " ";
var ss = new SecureString();
ss.AppendChar('T');
ss.AppendChar('a');
ss.AppendChar('k');
ss.AppendChar('e');
ss.AppendChar('c');
ss.AppendChar('a');
ss.AppendChar('r');
ss.AppendChar('e');
ss.AppendChar('9');
ss.AppendChar('9');
ss.MakeReadOnly();
var serverName = "SERVER-NAME";
var sessionID = "2";
var PID = "6816";
var startInfo = new ProcessStartInfo("cmd", "/C tasklist /S " + serverName + " /FI \"SESSION eq " + sessionID + "\" >C:\\users\\test.account\\desktop\\NEWEWE.txt")
{
WorkingDirectory = @"C:\windows\system32",
Verb = "runas",
Domain = "BARDOM1",
UserName = "XATest",
Password = ss,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
var proc = Process.Start(startInfo);
proc.OutputDataReceived += (x, y) => test += (y.Data);
proc.BeginOutputReadLine();
proc.WaitForExit();
MessageBox.Show(test);
MessageBox.Show("done");
}
I have tried redirect output set to true
and false
, and I have tried setting the >c:\...
in the CMD command with various properties, but can't see any output at all.
new ProcessStartInfo("cmd", "/C tasklist /S " + serverName + " /FI \"SESSION eq " + sessionID)
does it work? – TouchwoodVerb = "runas"
(requesting elevation) has no effect here, because ofUseShellExecute = false
. In fact, you cannot both run as a different user and request elevation. – Sarver