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 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.
检查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):
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
我们过去通过以编程方式使用命令行执行我们的进程来完成此操作。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.