Solved

detecting the ouput from the command prompt?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now