Convert Console.WriteLine to text field

Console.WriteLine("Network adapter: {0}", adapters[i].Name); Console.WriteLine(" Status: {0}", adapters[i].OperationalStatus.ToString()); Console.WriteLine(" Interface: {0}", adapters[i].NetworkInterfaceType.ToString()); Console.WriteLine(" Description: {0}", adapters[i].Description); Console.WriteLine(" ID: {0}", adapters[i].Id); Console.WriteLine(" Speed: {0}", adapters[i].Speed); Console.WriteLine(" SupportsMulticast: {0}", adapters[i].SupportsMulticast); Console.WriteLine(" IsReceiveOnly: {0}", adapters[i].IsReceiveOnly); Console.WriteLine(" MAC: {0}", adapters[i].GetPhysicalAddress().ToString()); 

I want this Console.WriteLine to appear in the text field of the form, but had no idea how to do this.

+4
source share
4 answers

If I liked it, you didn’t like the StringBuilder solution, check out the Sébastien blog

How to redirect console output in TextBox to C #

+4
source

Just make sure the text box is set to multi-line:

 StringBuilder sb = new StringBuilder(); sb.AppendLine(string.Format("Network adapter: {0}", adapters[i].Name)); sb.AppendLine(string.Format(" Status: {0}", adapters[i].OperationalStatus.ToString())); sb.AppendLine(string.Format(" Interface: {0}", adapters[i].NetworkInterfaceType.ToString())); sb.AppendLine(string.Format(" Description: {0}", adapters[i].Description)); sb.AppendLine(string.Format(" ID: {0}", adapters[i].Id)); sb.AppendLine(string.Format(" Speed: {0}", adapters[i].Speed)); sb.AppendLine(string.Format(" SupportsMulticast: {0}", adapters[i].SupportsMulticast)); sb.AppendLine(string.Format(" IsReceiveOnly: {0}", adapters[i].IsReceiveOnly)); sb.AppendLine(string.Format(" MAC: {0}", adapters[i].GetPhysicalAddress().ToString())); textBox1.Text = sb.ToString(); 
+3
source

You can try reading the current process' stdout , for example, here: System.Diagnostics.Process.GetCurrentProcess().StandardOutput

0
source

If you want to connect an existing console application to a TextBox, here is something similar when I transferred content from a process to the console. http://www.bryancook.net/2008/03/redirect-standard-output-of-service-to.html

The difference between this example and what you want to do is change the registration to insert data into the text box.

But be careful! You can only add output to the TextBox from the stream on which it was created (user interface stream). You will need to march data from another stream to the user interface stream.

See this answer , which has an excellent extension method for passing data to the user interface.

As a result, you will receive:

 private static void process_OutputDataReceived(object sender, DataReceivedEventArgs e) { if (!String.IsNullOrEmpty(e.Data)) { _textBox.InvokeIfRequired( c => c.Text += e.Data ); } } 
0
source

Source: https://habr.com/ru/post/1342641/


All Articles