简体   繁体   English

使用Process.Start时如何隐藏控制台应用程序用户界面?

[英]How do I hide a console application user interface when using Process.Start?

I want to run a console application that will output a file. 我想运行一个输出文件的控制台应用程序。

I user the following code: 我使用以下代码:

Process barProcess = Process.Start("bar.exe", @"C:\foo.txt");

When this runs the console window appears. 运行时,将出现控制台窗口。 I want to hide the console window so it is not seen by the user. 我想隐藏控制台窗口,以便用户看不到它。

Is this possible? 这可能吗? Is using Process.Start the best way to start another console application? 使用Process.Start是启动另一个控制台应用程序的最佳方法吗?

        Process p = new Process();
        StreamReader sr;
        StreamReader se;
        StreamWriter sw;

        ProcessStartInfo psi = new ProcessStartInfo(@"bar.exe");
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;
        psi.RedirectStandardInput = true;
        psi.CreateNoWindow = true;
        p.StartInfo = psi;
        p.Start();

This will start a child process without displaying the console window, and will allow the capturing of the StandardOutput, etc. 这将启动子进程而不显示控制台窗口,并允许捕获StandardOutput等。

检查ProcessStartInfo并设置WindowStyle = ProcessWindowStyle.Hidden和CreateNoWindow = true。

If you would like to retrieve the output of the process while it is executing, you can do the following (example uses the 'ping' command): 如果要在执行过程中检索进程的输出,可以执行以下操作(示例使用'ping'命令):

var info = new ProcessStartInfo("ping", "stackoverflow.com") {
    UseShellExecute = false, 
    RedirectStandardOutput = true, 
    CreateNoWindow = true 
};
var cmd = new Process() { StartInfo = info };
cmd.Start();
var so = cmd.StandardOutput;
while(!so.EndOfStream) {
    var c = ((char)so.Read()); // or so.ReadLine(), etc
    Console.Write(c); // or whatever you want
}
...
cmd.Dispose(); // Don't forget, or else wrap in a using statement

我们过去通过以编程方式使用命令行执行我们的进程来完成此操作。

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

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