Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Console Commands from Windows App

Posted on 2004-09-27
4
Medium Priority
?
419 Views
Last Modified: 2010-04-15
The following code runs when I press a command button in a Windows App:

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd";
process.StartInfo.Arguments = "/c nbtstat -a COMPUTERNAME";
process.StartInfo.UseShellExecute = false;
process.Start();

When the console opens it quickly executes the "nbtstat -a computer" command and then disappears.  How do I keep that console window open to see the return results of the nbtstat command, and then close it on a keypress?  Something like, getch() in C++ I'd guess....?

Regards,
Tony
0
Comment
Question by:fattumsdad
  • 2
4 Comments
 
LVL 19

Assisted Solution

by:drichards
drichards earned 400 total points
ID: 12161809
Tell the console to pause:

process.StartInfo.Arguments = "/c nbtstat -a COMPUTERNAME & pause";
0
 
LVL 69

Expert Comment

by:Callandor
ID: 12161856
You can use Console.ReadLine() to get input.
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 1600 total points
ID: 12161899
drichards has an excellent suggestion.  You could also do this:

  System.Diagnostics.Process process = new System.Diagnostics.Process();
  process.StartInfo.FileName = "cmd";
  process.StartInfo.Arguments = "/c nbtstat -a COMPUTERNAME";
  process.StartInfo.UseShellExecute = false;
  process.StartInfo.CreateNoWindow = true;
  process.StartInfo.RedirectStandardOutput = true;
  process.Start();
  string processOutput = process.StandardOutput.ReadToEnd();

process.StandardOutput.ReadToEnd() will wait until the process finishes, and then processOutput will contain everything that would have been printed to the console window.  Then you can use it however you like in your application:

  System.Windows.Forms.MessageBox.Show(processOutput.Replace("\r\r\n", Environment.NewLine));
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 12162073
If you don't like the console window, you can always create your own:

    static System.Windows.Forms.Form GetOutputForm(string command)
    {
      System.Diagnostics.Process process = new System.Diagnostics.Process();
      process.StartInfo.FileName = "cmd";
      process.StartInfo.Arguments = "/c " + command;
      process.StartInfo.UseShellExecute = false;
      process.StartInfo.CreateNoWindow = true;
      process.StartInfo.RedirectStandardOutput = true;
      process.Start();

      string processOutput = process.StandardOutput.ReadToEnd();

      System.Windows.Forms.Form outputForm = new System.Windows.Forms.Form();
      outputForm.Text = "Console Output";
      outputForm.Size = new System.Drawing.Size(640, 480);

      System.Windows.Forms.TextBox textBox = new System.Windows.Forms.TextBox();
      textBox.Text = processOutput.Replace("\r\r\n", Environment.NewLine).Trim();
      textBox.BackColor = System.Drawing.Color.Black;
      textBox.ForeColor = System.Drawing.Color.Lime;
      textBox.Font = new System.Drawing.Font("Courier New", 8f);
      textBox.Dock = System.Windows.Forms.DockStyle.Fill;
      textBox.Multiline = true;
      textBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;

      textBox.Select(textBox.Text.Length, 0);
     
      outputForm.Controls.Add(textBox);

      return outputForm;
    }

And then you can do:

  GetOutputForm("net send * Hello World!").ShowDialog();

or

  GetOutputForm("nbtstat -a COMPUTERNAME").ShowDialog();

or even,

  GetOutputForm("nbtstat -a COMPUTERNAME");
  to execute the process but forget about the output.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question