Vinh La Kiến

48 bundles
1 file4 months ago
1

C# Sub-process demo code

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ProcessDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ProcessStartInfo psi = new ProcessStartInfo(); psi.CreateNoWindow = true; psi.UseShellExecute = false; psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; // psi.FileName = "ping"; // psi.Arguments = "google.com"; psi.FileName = "xcopy"; psi.Arguments = "D:/Dir1 E:/Dir2"; Process process = new Process(); process.StartInfo = psi; process.EnableRaisingEvents = true; process.OutputDataReceived += Process_OutputDataReceived; process.ErrorDataReceived += Process_ErrorDataReceived; process.Exited += Process_Exited; process.Start(); process.BeginOutputReadLine(); process.WaitForExit(); } private void Process_Exited(object sender, EventArgs e) { Console.WriteLine("END"); } private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e) { Console.WriteLine("Error"); Console.WriteLine(e.Data); } private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) { Console.WriteLine(e.Data); } } }