logo

How to capture execution output using Shell()?

Jack Bishop replied

31-Aug-04 05:49 PM

Howdy,

I'm trying to capture the output of a command invoked with Shell(). For example, 

Shell("dir c:\", vbMinimizedNoFocus) or, more helpful,

Shell("telnet baz", vbMinimizedNoFocus).

I've attempted playing some redirection games with the local DOS command thinking I can work around some of the resulting limitations but no go. I'm trying to invoke a command on a *nix server and capture its output for further analysis/ investigation. I can invoke a dos command (ergo the "dir c:\" example) to accomplish my goal.

TIA for suggestions and guidance.
Reply Reply Using Power Editor
Peter Bromberg

Better to use the Process class

Peter Bromberg replied

31-Aug-04 06:49 PM

with the StartInfo methods, than to use the VB.NET "Crutch" classes from the VisualBasic namespaces:

Example (untested code, but you get the idea):

Process process = new Process();
    string FileName="telnet";
    string Arguments = "baz";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.FileName = FileName;
    process.StartInfo.Arguments = Arguments;
    process.StartInfo.WorkingDirectory = WorkingDirectory;
    process.Start(); 
    string output = process.StandardOutput.ReadToEnd();

VB.NET:
Dim process As New Process()
Dim FileName As String = "telnet"
Dim Arguments As String = "baz"
process.StartInfo.UseShellExecute = False
process.StartInfo.RedirectStandardOutput = True
process.StartInfo.RedirectStandardError = True
process.StartInfo.CreateNoWindow = True
process.StartInfo.FileName = FileName
process.StartInfo.Arguments = Arguments
process.StartInfo.WorkingDirectory = WorkingDirectory
process.Start()
Dim output As String = process.StandardOutput.ReadToEnd()


Biography
Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking, financial and telephony for over 20 years. Pete focuses exclusively on the .NET Platform, and currently develops SOA and other .NET applications for a Fortune 500 clientele. Peter enjoys producing digital photo collage with Maya,playing jazz flute, the beach, and fine wines. You can view Peter's UnBlog and IttyUrl sites. Pete Tweets at peterbromberg No Emails! Post it to the forums, I answer there!

Site Rank:  Not applicable - Current Winnings:  $0.00


Reply Reply Using Power Editor


digg facebook google buzz reddit del.icio.us stumble upon twitter

Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum










Pete's Resume  |  Robbe's Resume  |  Neado  |  Free Icons  |  Privacy  |   (c) 2010