943,902 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3796
  • C# RSS
May 12th, 2009
0

C# Command Prompt Reading

Expand Post »
I am trying to read a line from the command prompt and output that line to a text box in my C# application. The only catch is i can't seem to get it to read a line at a time and display it in the text box as the command prompt is running its command.

for example

string output = "";
while (output != null)
{
output = console.StandardOutput.ReadLine();
OutputBox.Text += '\n' + output;
}

Does anyone know how to read a command prompt line by line and output it into a text box as it is occurring?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
DevC++4.9.9.2 is offline Offline
39 posts
since Apr 2008
May 12th, 2009
0

Re: C# Command Prompt Reading

Use the following code :

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication5
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string s = "";
  13. Sample a = new Sample();
  14. a.Refresh(); // re-paint a form
  15. do
  16. {
  17. s = Console.ReadLine();
  18. a.MyText = s;
  19. a.Refresh(); // re-paint a form
  20. } while (s.Length != 0);
  21. }
  22. static void show()
  23. {
  24.  
  25. }
  26. }
  27. }
  28.  
  29. class Sample : System.Windows.Forms.Form
  30. {
  31. System.Windows.Forms.TextBox t = new System.Windows.Forms.TextBox();
  32. public Sample()
  33. {
  34. t.Multiline = true;
  35. t.Size = new System.Drawing.Size(100, 100);
  36. Controls.Add(t);
  37. Show();
  38. }
  39. public string MyText
  40. {
  41. get
  42. {
  43. return t.Text;
  44. }
  45. set
  46. {
  47. t.Text = t.Text + "\r" + "\n" + value;
  48. }
  49. }
  50.  
  51. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 13th, 2009
0

Re: C# Command Prompt Reading

Thank you, works perfect.


Click to Expand / Collapse  Quote originally posted by adatapost ...
Use the following code :

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication5
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string s = "";
  13. Sample a = new Sample();
  14. a.Refresh(); // re-paint a form
  15. do
  16. {
  17. s = Console.ReadLine();
  18. a.MyText = s;
  19. a.Refresh(); // re-paint a form
  20. } while (s.Length != 0);
  21. }
  22. static void show()
  23. {
  24.  
  25. }
  26. }
  27. }
  28.  
  29. class Sample : System.Windows.Forms.Form
  30. {
  31. System.Windows.Forms.TextBox t = new System.Windows.Forms.TextBox();
  32. public Sample()
  33. {
  34. t.Multiline = true;
  35. t.Size = new System.Drawing.Size(100, 100);
  36. Controls.Add(t);
  37. Show();
  38. }
  39. public string MyText
  40. {
  41. get
  42. {
  43. return t.Text;
  44. }
  45. set
  46. {
  47. t.Text = t.Text + "\r" + "\n" + value;
  48. }
  49. }
  50.  
  51. }
Reputation Points: 10
Solved Threads: 1
Light Poster
DevC++4.9.9.2 is offline Offline
39 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Thread
Next Thread in C# Forum Timeline: open folder





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC