c# .NEt 3.5 无法隐藏 CMD Window 当 运行 进程作为用户 - 表单应用程序
c# .NEt 3.5 Unable to Hide CMD Window When Running a Process as User - Form Application
我正在尝试 运行 使用我的支持应用程序中的 CMD 提示在后台运行一堆进程。
我已经为标准命令成功完成了此操作,但是当尝试以管理员身份 运行 命令(检索和结束进程)时,控制台 window 将短暂出现在屏幕上。
代码:
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;
}
}
据我所知,设置 shellExecute=false、createNoWindow=true 和 ProcessWindowStyle.Hidden 应该可以解决问题,但我认为由于需要管理员登录,所以无法正常工作。
有没有人知道这个问题的解决方案或合适的解决方法?
任何帮助深表感谢,
非常感谢
您可以使用以下代码:
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
static void Main(string[] args)
{
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
}
从 MSDN site 到 ProcessStartInfo.CreateNoWindow 属性 :
Remarks
If the UseShellExecute property is true or the UserName and Password
properties are not null, the CreateNoWindow property value is ignored
and a new window is created.
没有提到解决方法或解决方案,而且我在任何地方都找不到。
当 运行 某些进程(CreateNoWindow 属性 在不使用用户名和密码时工作)时,我不得不求助于我的应用程序短暂显示 CMD windows。
我正在尝试 运行 使用我的支持应用程序中的 CMD 提示在后台运行一堆进程。
我已经为标准命令成功完成了此操作,但是当尝试以管理员身份 运行 命令(检索和结束进程)时,控制台 window 将短暂出现在屏幕上。
代码:
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;
}
}
据我所知,设置 shellExecute=false、createNoWindow=true 和 ProcessWindowStyle.Hidden 应该可以解决问题,但我认为由于需要管理员登录,所以无法正常工作。
有没有人知道这个问题的解决方案或合适的解决方法? 任何帮助深表感谢, 非常感谢
您可以使用以下代码:
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
static void Main(string[] args)
{
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
}
从 MSDN site 到 ProcessStartInfo.CreateNoWindow 属性 :
Remarks
If the UseShellExecute property is true or the UserName and Password properties are not null, the CreateNoWindow property value is ignored and a new window is created.
没有提到解决方法或解决方案,而且我在任何地方都找不到。
当 运行 某些进程(CreateNoWindow 属性 在不使用用户名和密码时工作)时,我不得不求助于我的应用程序短暂显示 CMD windows。