Solved

Console Commands from Windows App

Posted on 2004-09-27
4
396 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 100 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 400 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now