Solved

detecting the ouput from the command prompt?

Posted on 2004-04-23
7
250 Views
Last Modified: 2010-04-24
i want to make form so that when i enter values in that form (obviously textbox or other) that go and write on the command prompt like i write 'dir' in the textbox it should write dir in the command prompt and when the command prompt gives the output , the output values should appear in the form fields in some list or something, e.g. if i write 'dir' in the text box it shows all the directories in the drive in the list box on the form, further can i arrange the ouput values in the list box as i want? i will be thankful if you people give me some coding or technique how can i handle this. thanks so much ,  i will be waiting for the answers.
0
Comment
Question by:baburkhan
[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
  • 4
7 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10914542
use the process object to start it (System.Diagnostics.Process)

There are Streams available for
StandardInput
StandardOutput
StandardError

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "test.exe";
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();


However doing this with Dir is a bad idea as you can use the directory objects and file objects to get all of this information without having to go through all of the trouble to parse the output etc. Take a look at ... System.IO.Directory




0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10914545
use the process object to start it (System.Diagnostics.Process)

There are Streams available for
StandardInput
StandardOutput
StandardError

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "test.exe";
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();


However doing this with Dir is a bad idea as you can use the directory objects and file objects to get all of this information without having to go through all of the trouble to parse the output etc. Take a look at ... System.IO.Directory




0
 

Author Comment

by:baburkhan
ID: 10920340
thanks , well what if i do use the 'shell' i can use the system dot come and dot exe exentention like ping but i want them to print the output result at my form not in the console.
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 20 total points
ID: 10920582
cmd.exe /c internalcommand

cmd.exe /c dir for example ...

other than that just run the exe ..

if its a very long running command you probably want to spawn a seperate thread to read the info as it comes instead of waiting until its done.

using System;
using System.Diagnostics ;
namespace ConsoleApplication4
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  Process p = new Process();
                  p.StartInfo.UseShellExecute = false;
                  p.StartInfo.RedirectStandardOutput = true;
                  p.StartInfo.Arguments =  " 192.168.1.1";
                  p.StartInfo.FileName = @"C:\Windows\System32\ping.exe";
                  p.StartInfo.WorkingDirectory = "C:\\" ;
                  p.Start();
                  p.WaitForExit();
                  string output = p.StandardOutput.ReadToEnd();
                  Console.WriteLine("Got Directory info\n" + output) ;
            }
      }
}
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10920587
oops got ping nfo not got directory :)
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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

623 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