Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string output = process.StandardOutput.ReadToEnd();
- using System;
- using System.Diagnostics;
- namespace InteractWithConsoleApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
- cmdStartInfo.FileName = @"C:WindowsSystem32cmd.exe";
- cmdStartInfo.RedirectStandardOutput = true;
- cmdStartInfo.RedirectStandardError = true;
- cmdStartInfo.RedirectStandardInput = true;
- cmdStartInfo.UseShellExecute = false;
- cmdStartInfo.CreateNoWindow = true;
- Process cmdProcess = new Process();
- cmdProcess.StartInfo = cmdStartInfo;
- cmdProcess.ErrorDataReceived += cmd_Error;
- cmdProcess.OutputDataReceived += cmd_DataReceived;
- cmdProcess.EnableRaisingEvents = true;
- cmdProcess.Start();
- cmdProcess.BeginOutputReadLine();
- cmdProcess.BeginErrorReadLine();
- cmdProcess.StandardInput.WriteLine("ping google.com.ua"); //Execute ping bing.com
- cmdProcess.StandardInput.WriteLine("exit"); //Execute exit.
- cmdProcess.WaitForExit();
- }
- static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
- {
- Console.WriteLine("Output from other process");
- Console.WriteLine(e.Data);
- }
- static void cmd_Error(object sender, DataReceivedEventArgs e)
- {
- Console.WriteLine("Error from other process");
- Console.WriteLine(e.Data);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement