C# 如何使用c隐藏命令窗口#
构建将执行exe文件(pagesnap.exe)的控制台应用程序。我想在执行过程中隐藏它的窗口(pagesnap.exe)。这是怎么做到的C# 如何使用c隐藏命令窗口#,c#,.net,command-prompt,psexec,C#,.net,Command Prompt,Psexec,构建将执行exe文件(pagesnap.exe)的控制台应用程序。我想在执行过程中隐藏它的窗口(pagesnap.exe)。这是怎么做到的 ProcessStartInfo Psi = new ProcessStartInfo("D:\\PsTools\\"); Psi.FileName = "D:\\PsTools\\psexec.exe"; Psi.Arguments = @"/C \\DESK123 D:\files\pagesnap.exe"; Psi.UseShellExecu
ProcessStartInfo Psi = new ProcessStartInfo("D:\\PsTools\\");
Psi.FileName = "D:\\PsTools\\psexec.exe";
Psi.Arguments = @"/C \\DESK123 D:\files\pagesnap.exe";
Psi.UseShellExecute = false;
Psi.RedirectStandardOutput = true;
Psi.RedirectStandardInput = true;
Psi.WindowStyle = ProcessWindowStyle.Hidden;
Psi.CreateNoWindow = true;
Process.Start(Psi).WaitForExit();
DESK123是本地PC。稍后将在远程PC上尝试此操作
我尝试过的事情
Psi.Arguments = @"/C start /b psexec \\DESK123 D:\files\pagesnap.exe";
Psi.Arguments = @"/b psexec \\DESK123 D:\files\pagesnap.exe";
Psi.Arguments = @"/C psexec \\DESK123 /b D:\files\pagesnap.exe";
Psi.Arguments = @"/C psexec \\DESK123 D:\files\pagesnap.exe 2>&1 output.log";
更新:我已经将输出类型的pagesnap构建为windows应用程序,而不是控制台。cmd窗口现在无法打开。似乎这是我唯一的方法只需调用以下函数。将参数作为命令和工作目录传递
private string BatchCommand(string cmd, string mapD)
{
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + cmd);
procStartInfo.WorkingDirectory = mapD;
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.RedirectStandardInput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process cmdProcess = new System.Diagnostics.Process();
cmdProcess.StartInfo = procStartInfo;
cmdProcess.ErrorDataReceived += cmd_Error;
cmdProcess.OutputDataReceived += cmd_DataReceived;
cmdProcess.EnableRaisingEvents = true;
cmdProcess.Start();
cmdProcess.BeginOutputReadLine();
cmdProcess.BeginErrorReadLine();
cmdProcess.StandardInput.WriteLine("ping www.google.com"); //Execute ping
cmdProcess.StandardInput.WriteLine("exit"); //Execute exit.
cmdProcess.WaitForExit();
// Get the output into a string
return Batchresults;
}
static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
Batchresults += Environment.NewLine + e.Data.ToString();
}
void cmd_Error(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
Batchresults += Environment.NewLine + e.Data.ToString();
}
}
看到这一条:@AmittaiShapira我确实读过,但不知道在哪里使用
/b
。我尝试了@/b psexec
和”/b D:\files\pagesnap.exe“
。两个都没用,你想干什么?如果确实要执行psexec
,则没有理由执行cmd
。选中@PanagiotisKanavos。我已经更新了我的问题。仅当PageSnap构建为windows应用程序而不是控制台应用程序时,cmd窗口才会显示。@Qwery我认为您混淆了什么在哪里执行以及显示什么。ProcessInfo控制如何运行psexec
,而不是pagesnap。要控制
pagesnap`的显示方式,您应该将适当的参数传递给psexec
,例如-d
,对于非交互式,bAttchResults是一个stringI类型的全局变量。我刚刚尝试了您的解决方案,但它仍然打开了pagesnap的命令窗口。它非常适合我。procStartInfo.CreateNoWindow=true;行隐藏命令窗口。/C psexec\\DESK123 D:\files\pagesnap.exe将其作为参数传递。相反,尝试使用/C psexec\\DESK123 D:\files\pagesnap.exe 2>&1 Logfile.txt作为参数,它将隐藏窗口并将输出写入logfileLet。