Solved

Executing a file and capturing the output

Posted on 2004-08-14
3
384 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

912 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

23 Experts available now in Live!

Get 1:1 Help Now