Solved

Executing a file and capturing the output

Posted on 2004-08-14
3
387 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 Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

679 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