I am trying to run a bunch of processes in the background using the CMD prompt from my support application.
I have successfully done this for standard commands, but when trying to run a command as an administrator (to retrieve and end processes), the console window will briefly appear on the screen.
Code:
public static bool checkRunning(string procName)
{
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();
//ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/C tasklist /S " + Program.servName + " /FI \"SESSION eq " + Program.sessID + "\" /FO CSV /NH")
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C tasklist /S " + Program.servName + " /FI \"SESSION eq " + Program.sessID + "\" /FO CSV /NH";
startInfo.WorkingDirectory = @"C:\windows\system32";
startInfo.Verb = "runas";
startInfo.Domain = "mydomain";
startInfo.UserName = "ADMINuser";
startInfo.Password = ss;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
string strCheck = " ";
Process proc = Process.Start(startInfo);
proc.OutputDataReceived += (x, y) => strCheck += y.Data;
proc.BeginOutputReadLine();
proc.WaitForExit();
if (strCheck.Contains(procName))
{
return true;
}
else
{
return false;
}
}
From what I understand, setting shellExecute=false, createNoWindow=true and ProcessWindowStyle.Hidden should resolve the issue, but I believe it is not working due to the required admin log in.
Does anyone know of a solution or suitable workaround to this problem? Any help is much appreciated, Many thanks