Executing a file and capturing the output

Posted on 2004-08-14
Last Modified: 2008-01-09
This is really a two part question:

1.)  How do I execute an executable file from a c# program?  In c++ I could just use the ShellExecute windows API function call.  I really don't want to use API function call I would rather use something within the .NET framework...but if I do have to use the ShellExecute API function call; how do I do that using C#?

2.) Assuming the executable that I am executing from step 1 is a console application or something that outputs strictly text; how can I capture this text.  For instance how could I place it in a System.IO.TextReader.  In C++ I would have to pipe the output to a stream.  In C# is there something similar.

As much code examples would be extremely helpfull as I am new to C#...just recently converted from C++.

Question by:PerryDK
  • 2

Accepted Solution

NTAC earned 500 total points
ID: 11802404
Here is a quick example I put together to show you how to do this:

using System;
using System.Diagnostics;
using System.IO;

namespace ProcessTest
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main(string[] args)
                  Process process = new Process();
                  process.StartInfo.FileName = "TextOutputter.exe";   // THis will be your program
                  process.StartInfo.UseShellExecute = false;
                  process.StartInfo.RedirectStandardOutput = true;

                  StreamWriter sw = new StreamWriter("Output.txt");  

                          // This is the file you are going to be piping the output to.  If it is going to be a very large amount of data, use the commented lines here:
//                  string inLine;
//                  while( process.StandardOutput.Peek() > -1 )
//                        sw.WriteLine( process.StandardOutput.ReadLine());

                  string inLine = process.StandardOutput.ReadToEnd();

Author Comment

ID: 11802522
Ok thanks for quick response but why does

process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

Not prevent the Dos Window from opening up.  I have tested and If I use the shellExecute = true but then comment out all the StanderdOutput stuff the window doesn't show.

If I use the ShellExecute = true I get an error when using the StandardOutput Stuff.

Author Comment

ID: 11802524
Never mind I guess I need to use
process..StartInfo.CreateNoWindow = true;

Thanks for the quick reply

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

809 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