Solved

Console Commands from Windows App

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

630 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