C#
C++
SQL

0 Comments:

Post a Comment



<< Home

Shell Execute with output

Using the System.Diagnostics.Process class you can get a stream to read the output of the application you are executing. For example, you could execute "dir" to list get the directory contents of the working directory specified as a string.

Here's a basic class I wrote that allows you to get the standard output:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace Kahnadex{
    class WindowsHelper{
        public static Exception ShellExecute(string in_WorkingDirectory,string in_FileName,string in_Args,out string out_StdOut){
            Process p = new System.Diagnostics.Process();
            p.StartInfo.WorkingDirectory = in_WorkingDirectory;
            p.StartInfo.FileName = in_FileName;
            p.StartInfo.Verb = "open";
            p.StartInfo.Arguments = in_Args;

            //required to capture standard output
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
            p.Start();
            p.WaitForExit();

            //read the command line output
            try{
                StreamReader sr = p.StandardOutput;
                out_StdOut = sr.ReadToEnd();
                return(null);
            }catch(Exception ex){
                out_StdOut = "";
                return(ex);
            }
        }
    }
}

Labels: ,

posted by Brian at