如何使用c#隐藏命令窗口
本文关键字:命令 窗口 隐藏 何使用 | 更新日期: 2023-09-27 18:10:44
构建一个控制台应用程序,将执行一个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.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();
}
}