Solved

detecting the ouput from the command prompt?

Posted on 2004-04-23
7
248 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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