Solved

Console Commands from Windows App

Posted on 2004-09-27
4
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linq, c# 8 52
parse string in c# 5 41
C# LINQ 5 45
How to compare 2 values where one value contains the directory path in an ASP.NET application using C#? 2 31
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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