options for shelling out command line commands

D

djc

I'm wondering what options are available, if any, for being able to run 'any
command line command' from a .net compiled program. I would want the output
that would normally go to the command line for the command to be available
to my program.

I know from even very old VB that you can 'shell' out and run programs, one
of which could be for example, cmd.exe with specific command but I don't
know that you could get the actual command results (other than an exit, or
return code). Plus, before I go 'shelling out' I wanted to see if there is a
'newer' way of doing this. Maybe something more builtin like a class that
actually exposes an interface to send standard shell commands to and returns
the results to you in code? The actual output that would appear on the
screen if you had run the command from the command line.

eg.
myVariable = SomeCoolClass.ExecShellCmd("dir c:")

and the directory listing for c: would be a string in myVariable (including
white space chars to preserve formatting)

anyone know of anything like this? Buitlin to .net already?
 
M

Michael Nemtsev

Hello djc,

The only way is to redirect output of the console program to file and read
result afterwards

d> I'm wondering what options are available, if any, for being able to
d> run 'any command line command' from a .net compiled program. I would
d> want the output that would normally go to the command line for the
d> command to be available to my program.
d>
d> I know from even very old VB that you can 'shell' out and run
d> programs, one of which could be for example, cmd.exe with specific
d> command but I don't know that you could get the actual command
d> results (other than an exit, or return code). Plus, before I go
d> 'shelling out' I wanted to see if there is a 'newer' way of doing
d> this. Maybe something more builtin like a class that actually exposes
d> an interface to send standard shell commands to and returns the
d> results to you in code? The actual output that would appear on the
d> screen if you had run the command from the command line.
d>
d> eg.
d> myVariable = SomeCoolClass.ExecShellCmd("dir c:")
d> and the directory listing for c: would be a string in myVariable
d> (including white space chars to preserve formatting)
d>
d> anyone know of anything like this? Buitlin to .net already?
d>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
C

Chris Dunaway

You don't have to redirect to a file. When using the Process class,
you can redirect the output of the console program to a stream and then
just read that stream to get the output from the program.
 
D

djc

thats one way I was thinking could work. That or creating an environment
variable to redirect to and subsequently retrieve from.

thanks for the reply.
 
D

djc

that sounds perfect! Thanks.

are you refering to the same class as Patrick Steele is in his reply to this
post?
System.Diagnostics.Process AND System.Diagnostics.ProcessStartInfo?

I'll check this out tomorrow, I just wanted to clarify these are the same
solution and not 2 different ones.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top