Solved

Executing a file and capturing the output

Posted on 2004-08-14
3
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

622 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