简体   繁体   English

如何使用C#隐藏命令窗口

[英]How to hide the Command Window using c#

Building a console app that will execute an exe file(pagesnap.exe). 构建将执行exe文件(pagesnap.exe)的控制台应用程序。 I would like to hide its window(pagesnap.exe) during execution. 我想在执行期间隐藏其窗口(pagesnap.exe)。 How is it done. 怎么做

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 is the local PC. DESK123是本地PC。 Would later try this with remote PCs. 稍后将使用远程PC尝试此操作。

Things I have tried 我尝试过的事情

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";

Update: I have built pagesnap with Output type as a windows application instead of console. 更新:我已经将Outputn类型的pagesnap构建为Windows应用程序而不是控制台。 The cmd window doesn't come up, now. 现在,cmd窗口没有出现。 Seems this is the only way for me 看来这是我的唯一方法

Simply call the following function. 只需调用以下函数。 Pass the argument as your command and your working directory 将参数作为命令和工作目录传递

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

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM