Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you create the <code>Process</code> instance for tracert, you need to set the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.useshellexecute.aspx" rel="nofollow noreferrer"><code>ProcessStartInfo.UseShellExecute</code></a> to <code>false</code> and <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx" rel="nofollow noreferrer"><code>ProcessStartInfo.RedirectStandardOutput</code></a> to <code>true</code>. This will allow you to use <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx" rel="nofollow noreferrer"><code>Process.StandardOutput</code></a> property to read the output.</p> <p>You can use <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx" rel="nofollow noreferrer"><code>Process.BeginOutputReadLine</code></a> to start asynchronous reading and add an event handler to <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.outputdatareceived.aspx" rel="nofollow noreferrer"><code>Process.OutputDataReceived</code></a>.</p> <p><strong>Update:</strong> Here's an example WPF program that does what you want.</p> <p>MainWindow.xaml:</p> <pre><code>&lt;Window x:Class="Console.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Closed="Window_Closed"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;ScrollViewer Name="outputViewer" SizeChanged="ScrollViewer_SizeChanged" &gt; &lt;TextBlock Name="output" /&gt; &lt;/ScrollViewer&gt; &lt;TextBox Grid.Row="1" Name="input" KeyDown="input_KeyDown" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>MainWindow.cs:</p> <pre><code>using System; using System.Diagnostics; using System.Windows; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; namespace Console { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { ProcessStartInfo cmdStartInfo = new ProcessStartInfo("cmd.exe"); cmdStartInfo.CreateNoWindow = true; cmdStartInfo.RedirectStandardInput = true; cmdStartInfo.RedirectStandardOutput = true; cmdStartInfo.RedirectStandardError = true; cmdStartInfo.UseShellExecute = false; cmdStartInfo.WindowStyle = ProcessWindowStyle.Hidden; _cmd = new Process(); _cmd.StartInfo = cmdStartInfo; if (_cmd.Start() == true) { _cmd.OutputDataReceived += new DataReceivedEventHandler(_cmd_OutputDataReceived); _cmd.ErrorDataReceived += new DataReceivedEventHandler(_cmd_ErrorDataReceived); _cmd.Exited += new EventHandler(_cmd_Exited); _cmd.BeginOutputReadLine(); _cmd.BeginErrorReadLine(); } else { _cmd = null; } } private void Window_Closed(object sender, EventArgs e) { if ((_cmd != null) &amp;&amp; (_cmd.HasExited != true)) { _cmd.CancelErrorRead(); _cmd.CancelOutputRead(); _cmd.Close(); _cmd.WaitForExit(); } } void _cmd_OutputDataReceived(object sender, DataReceivedEventArgs e) { UpdateConsole(e.Data); } void _cmd_ErrorDataReceived(object sender, DataReceivedEventArgs e) { UpdateConsole(e.Data, Brushes.Red); } void _cmd_Exited(object sender, EventArgs e) { _cmd.OutputDataReceived -= new DataReceivedEventHandler(_cmd_OutputDataReceived); _cmd.Exited -= new EventHandler(_cmd_Exited); } private void ScrollViewer_SizeChanged(object sender, SizeChangedEventArgs e) { outputViewer.ScrollToBottom(); } private void input_KeyDown(object sender, KeyEventArgs e) { switch (e.Key) { case Key.Enter: _cmd.StandardInput.WriteLine(input.Text); input.Text = ""; break; case Key.Escape: input.Text = ""; break; } } private void UpdateConsole(string text) { UpdateConsole(text, null); } private void UpdateConsole(string text, Brush color) { if (!output.Dispatcher.CheckAccess()) { output.Dispatcher.Invoke( new Action( () =&gt; { WriteLine(text, color); } ) ); } else { WriteLine(text, color); } } private void WriteLine(string text, Brush color) { if (text != null) { Span line = new Span(); if (color != null) { line.Foreground = color; } foreach (string textLine in text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) { line.Inlines.Add(new Run(textLine)); } line.Inlines.Add(new LineBreak()); output.Inlines.Add(line); } } Process _cmd; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload