Solved

detecting the ouput from the command prompt?

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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