0

What I want to do is run a command in C# through a console command window. What I want this command to do is run an existing exe file with my given input and print the output to a different file.

I have this code:

System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
string l_sCommand = string.Empty;
l_sCommand += "exe file" + "<" + "Input txt file" + ">" + "output txt file";

System.Diagnostics.ProcessStartInfo procStartInfo =
  new System.Diagnostics.ProcessStartInfo("cmd", "/c " + l_sCommand);

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();

but this doesn't work. does anyone have any idea why?

TrueWill
  • 25,132
  • 10
  • 101
  • 150
Yarden
  • 25
  • 4

5 Answers5

0

Why not use:

System.Diagnostics.Process.Start("<exe file>", "<args>");

You're trying to launch cmd.exe? If it's a command line app, you can just launch the EXE from the Process.Start static method.

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
  • I'll ask the question in a different way. What I want to do is run a command, as if I were running a batch file - but I don't want to create the batch file" – Yarden Nov 24 '10 at 18:35
  • I see. So what isn't working with your current situation? Is it erroring out, or is it not creating the output file, or something else? – David Hoerster Nov 24 '10 at 18:42
  • It opens a console window for a second and then the windows closes and the output file is not created. - it is not erroring. – Yarden Nov 24 '10 at 18:59
0

use Process.Start method directly passing in the arguments or use the builtin Shell function

Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66
0

Normally you just pass the executable filename to the Process.Start method or use the Process.StartInfo property. But if you want to redirect the standard output, you need to set Process.StartInfo.RedirectStandardOutput to true and read Process.StandardOutput when the process ends. See below sample code lifter from MSDN.

// Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

For more information check the documentation at this link. http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx

jake.stateresa
  • 226
  • 2
  • 3
0

A common enough problem actually. The Process class just isn't as easy to use as it should be. (see this post) The following code will work if you use the ProcessRunner class I wrote.

using CSharpTest.Net.Processes;
void Exec(string outputFile, Encoding encoding, string program, params string[] args)
{
    using (TextWriter output = new StreamWriter(outputFile, false, encoding))
    using (ProcessRunner pi = new ProcessRunner(program))
    {
        pi.OutputReceived += delegate(object sender, ProcessOutputEventArgs e)
                                 { output.WriteLine(e.Data); };
        pi.Run(args);
    }
}
csharptest.net
  • 62,602
  • 11
  • 71
  • 89
0

Why not stream the output to a buffer and then write it yourself?

public void CaptureProcess(String Command, String Arguments, String Filename)
{
    // This is the code for the base process
    Process myProcess = new Process();
    myProcess.StartInfo = new ProcessStartInfo(Command, Arguments);
    myProcess.StartInfo.UseShellExecute = false;
    myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    myProcess.StartInfo.RedirectStandardOutput = true;

    // Open a filestream for the output
    using (FileStream fs = new FileStream(Filename, FileMode.OpenOrCreate))
    {
        using (StreamWriter sw = new StreamWriter(fs))
        {

            // open the stream and capture all output from the process until it dies.
            using (StreamReader ProcessOutput = myProcess.StandardOutput)
            {
                myProcess.Start();
                string output = ProcessOutput.ReadToEnd();
                sw.Write(output);

                myProcess.WaitForExit();
                myProcess.Close();
            }
        }
    }
}
Greg Buehler
  • 3,897
  • 3
  • 32
  • 39