Solved

Executing a file and capturing the output

Posted on 2004-08-14
3
383 Views
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++.

Thanks.
0
Comment
Question by:PerryDK
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
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>
            [STAThread]
            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;

                  process.Start();
                  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();
                  process.WaitForExit();
                  sw.WriteLine(inLine);
                  sw.Close();
            }
      }
}
0
 
LVL 4

Author Comment

by:PerryDK
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.
0
 
LVL 4

Author Comment

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

Thanks for the quick reply
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now