Sheng Xengg
Sheng Xengg

Reputation: 125

Convert Console.WriteLine into textbox

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());

What I want is to have this Console.WriteLine displayed in a text box on the form but don't have any idea on how to do it.

Upvotes: 4

Views: 15069

Answers (4)

RQDQ
RQDQ

Reputation: 15569

Just make sure that the text box is set to multiline:

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();

Upvotes: 3

little bandit
little bandit

Reputation: 41

If like me you didn't like the StringBuilder solution check out Sébastien's blog

How to redirect the Console’s output to a TextBox in C#

Upvotes: 4

bryanbcook
bryanbcook

Reputation: 18128

If you're looking to pipe an existing console application into a TextBox, here's something similar where I piped content from a process into a console. http://www.bryancook.net/2008/03/redirect-standard-output-of-service-to.html

The difference between that example and what you'd want to do is change the logging to push the data into the Textbox.

But beware! You can only append output to a TextBox from the thread that it was created on (the UI thread). You'll need to marshal the data from the other thread to the UI thread.

See this answer which has a great extension method to push data to the UI.

You'd end up with:

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

Upvotes: 0

Snowbear
Snowbear

Reputation: 17274

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

Upvotes: 0

Related Questions