【问题标题】:How to hide the Command Window using c#如何使用 C# 隐藏命令行窗口
【发布时间】:2015-09-02 07:05:20
【问题描述】:

构建将执行 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";

更新:我已经将输出类型的页面快照构建为 Windows 应用程序而不是控制台。现在 cmd 窗口没有出现。看来这是我唯一的方法了

【问题讨论】:

  • @AmittaiShapira 我确实经历过,但不知道在哪里使用/b。我试过@"/b psexec".. /b D:\files\pagesnap.exe"。都没有用。
  • 什么你想做什么?如果你真的想执行psexec,就没有理由执行cmd。检查this duplicate answer
  • @PanagiotisKanavos。我已经更新了我的问题。仅当 PageSnap 构建为 windows 应用程序而不是控制台应用程序时,cmd 窗口才会显示。
  • @Qwery 我认为您混淆了在何处执行什么以及显示什么。 ProcessInfo 控制您如何运行psexec,而不是pagesnap. To control how pagesnap` 看来您应该将适当的参数传递给psexec,例如-d 用于非交互式

标签: c# .net command-prompt psexec


【解决方案1】:

只需调用以下函数。将参数作为命令和工作目录传递

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();

        }
    }

【讨论】:

  • Batchresults 是一个字符串类型的全局变量
  • 我刚刚尝试了您的解决方案,它仍然会打开 pagesnap 的命令窗口
  • 它非常适合我。 procStartInfo.CreateNoWindow = true;行隐藏命令窗口。
  • /C psexec \\DESK123 D:\files\pagesnap.exe 您将其作为参数传递。而是尝试使用 /C psexec \\DESK123 D:\files\pagesnap.exe 2>&1 Logfile.txt 作为参数它将隐藏窗口并将输出写入日志文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 2015-09-13
  • 2022-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多