Solved

Console Commands from Windows App

Posted on 2004-09-27
4
406 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
List<PaisEntity> - show some 1 35
Please explain purpose of GZIP 4 56
Why use this lambda? 12 60
ASP.NET - Why is accordion not working? 3 44
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

679 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