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

时间:2023-01-26 20:45:57

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启动另一个控制台应用程序的最佳方法是什么?

4 个解决方案

#1


        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等。

#2


Check into ProcessStartInfo and set the WindowStyle = ProcessWindowStyle.Hidden and the CreateNoWindow = true.

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

#3


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", "*.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

#4


We have done this in the past by executing our processes using the Command Line programatically.

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

#1


        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等。

#2


Check into ProcessStartInfo and set the WindowStyle = ProcessWindowStyle.Hidden and the CreateNoWindow = true.

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

#3


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", "*.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

#4


We have done this in the past by executing our processes using the Command Line programatically.

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