23

This is my code:

Process pr2 = new Process();
pr2.StartInfo.FileName = "show-snps";
pr2.StartInfo.Arguments = @"-Clr -x 2 out.delta > out.snps";
pr2.Start();
pr2.WaitForExit();

show-snps writes an error. when I delete the part "> out.snps" everything is ok and it writes the result into the terminal, but I need to redirect it to the text file. How can I do this?

2
  • Wrong usage. Suggests to read manual Apr 27, 2013 at 20:33
  • 1
    But when I copy everything to the command line there is no error Apr 27, 2013 at 20:34

3 Answers 3

23

You can't redirect like that when you are starting the program like that. It requires a shell such as CMD.EXE to do that.

Instead, you need to set ProcessStartInfo.RedirectStandardOutput to a stream that you manage yourself.

There's an example here.

That example shows how to get the data that the console process creates via a StreamReader. For your requirement, you would read from that StreamReader and write to a FileStream output.

In case it's any help, here's a utility class I wrote years ago (for .Net 2) which might be instructive:

using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;


namespace ProcessUtilities
{
    /// <summary>
    /// Encapsulates an executable program.
    /// This class makes it easy to run a console app and have that app's output appear
    /// in the parent console's window, and to redirect input and output from/to files.
    /// </summary>
    /// <remarks>
    /// To use this class:
    /// (1) Create an instance.
    /// (2) Set the ProgramFileName property if a filename wasn't specified in the constructor.
    /// (3) Set other properties if required.
    /// (4) Call Run().
    /// </remarks>

    public class Executable
    {
        #region Constructor

        /// <summary>Runs the specified program file name.</summary>
        /// <param name="programFileName">Name of the program file to run.</param>

        public Executable(string programFileName)
        {
            ProgramFileName = programFileName;

            _processStartInfo.ErrorDialog            = false;
            _processStartInfo.CreateNoWindow         = false;
            _processStartInfo.UseShellExecute        = false;
            _processStartInfo.RedirectStandardOutput = false;
            _processStartInfo.RedirectStandardError  = false;
            _processStartInfo.RedirectStandardInput  = false;
            _processStartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
            _processStartInfo.Arguments              = "";
        }

            /// <summary>Constructor.</summary>

        public Executable(): this(string.Empty)
        {
        }

        #endregion  // Constructor

        #region Public Properties

        /// <summary>The filename (full pathname) of the executable.</summary>

        public string ProgramFileName
        {
            get
            {
                return _processStartInfo.FileName;
            }

            set
            {
                _processStartInfo.FileName = value;
            }
        }

        /// <summary>The command-line arguments passed to the executable when run. </summary>

        public string Arguments
        {
            get
            {
                return _processStartInfo.Arguments;
            }

            set
            {
                _processStartInfo.Arguments = value;
            }
        }

        /// <summary>The working directory set for the executable when run.</summary>

        public string WorkingDirectory
        {
            get
            {
                return _processStartInfo.WorkingDirectory;
            }

            set
            {
                _processStartInfo.WorkingDirectory = value;
            }
        }

        /// <summary>
        /// The file to be used if standard input is redirected,
        /// or null or string.Empty to not redirect standard input.
        /// </summary>

        public string StandardInputFileName
        {
            set
            {
                _standardInputFileName = value;
                _processStartInfo.RedirectStandardInput = !string.IsNullOrEmpty(value);
            }

            get
            {
                return _standardInputFileName;
            }
        }

        /// <summary>
        /// The file to be used if standard output is redirected,
        /// or null or string.Empty to not redirect standard output.
        /// </summary>

        public string StandardOutputFileName
        {
            set
            {
                _standardOutputFileName = value;
                _processStartInfo.RedirectStandardOutput = !string.IsNullOrEmpty(value);
            }

            get
            {
                return _standardOutputFileName;
            }
        }

        /// <summary>
        /// The file to be used if standard error is redirected,
        /// or null or string.Empty to not redirect standard error.
        /// </summary>

        public string StandardErrorFileName
        {
            set
            {
                _standardErrorFileName = value;
                _processStartInfo.RedirectStandardError = !string.IsNullOrEmpty(value);
            }

            get
            {
                return _standardErrorFileName;
            }
        }

        #endregion  // Public Properties

        #region Public Methods

        /// <summary>Add a set of name-value pairs into the set of environment variables available to the executable.</summary>
        /// <param name="variables">The name-value pairs to add.</param>

        public void AddEnvironmentVariables(StringDictionary variables)
        {
            if (variables == null)
                throw new ArgumentNullException("variables");

            StringDictionary environmentVariables = _processStartInfo.EnvironmentVariables;

            foreach (DictionaryEntry e in variables)
                environmentVariables[(string)e.Key] = (string)e.Value;
        }

        /// <summary>Run the executable and wait until the it has terminated.</summary>
        /// <returns>The exit code returned from the executable.</returns>

        public int Run()
        {
            Thread standardInputThread  = null;
            Thread standardOutputThread = null;
            Thread standardErrorThread  = null;

            _standardInput  = null;
            _standardError  = null;
            _standardOutput = null;

            int exitCode = -1;

            try
            {
                using (Process process = new Process())
                {
                    process.StartInfo = _processStartInfo;
                    process.Start();

                    if (process.StartInfo.RedirectStandardInput)
                    {
                        _standardInput = process.StandardInput;
                        standardInputThread = startThread(new ThreadStart(supplyStandardInput), "StandardInput");
                    }

                    if (process.StartInfo.RedirectStandardError)
                    {
                        _standardError = process.StandardError;
                        standardErrorThread = startThread(new ThreadStart(writeStandardError), "StandardError");
                    }

                    if (process.StartInfo.RedirectStandardOutput)
                    {
                        _standardOutput = process.StandardOutput;
                        standardOutputThread = startThread(new ThreadStart(writeStandardOutput), "StandardOutput");
                    }

                    process.WaitForExit();
                    exitCode = process.ExitCode;
                }
            }

            finally  // Ensure that the threads do not persist beyond the process being run
            {
                if (standardInputThread != null)
                    standardInputThread.Join();

                if (standardOutputThread != null)
                    standardOutputThread.Join();

                if (standardErrorThread != null)
                    standardErrorThread.Join();
            }

            return exitCode;
        }

        #endregion  // Public Methods

        #region Private Methods

        /// <summary>Start a thread.</summary>
        /// <param name="startInfo">start information for this thread</param>
        /// <param name="name">name of the thread</param>
        /// <returns>thread object</returns>

        private static Thread startThread(ThreadStart startInfo, string name)
        {
            Thread t = new Thread(startInfo);
            t.IsBackground = true ;
            t.Name = name;
            t.Start();
            return t;
        }

        /// <summary>Thread which supplies standard input from the appropriate file to the running executable.</summary>

        private void supplyStandardInput()
        {
            // feed text from the file a line at a time into the standard input stream

            using (StreamReader reader = File.OpenText(_standardInputFileName))
            using (StreamWriter writer = _standardInput)
            {
                writer.AutoFlush = true;

                for (;;)
                {
                    string textLine = reader.ReadLine();

                    if (textLine == null)
                        break;

                    writer.WriteLine(textLine);
                }
            }
        }

        /// <summary>Thread which outputs standard output from the running executable to the appropriate file.</summary>

        private void writeStandardOutput()
        {
            using (StreamWriter writer = File.CreateText(_standardOutputFileName))
            using (StreamReader reader = _standardOutput)
            {
                writer.AutoFlush = true;

                for (;;)
                {
                    string textLine = reader.ReadLine();

                    if (textLine == null)
                        break;

                    writer.WriteLine(textLine);
                }
            }

            if (File.Exists(_standardOutputFileName))
            {
                FileInfo info = new FileInfo(_standardOutputFileName);

                // if the error info is empty or just contains eof etc.

                if (info.Length < 4)
                    info.Delete();
            }
        }

        /// <summary>Thread which outputs standard error output from the running executable to the appropriate file.</summary>

        private void writeStandardError()
        {
            using (StreamWriter writer = File.CreateText(_standardErrorFileName))
            using (StreamReader reader = _standardError)
            {
                writer.AutoFlush = true;

                for (;;)
                {
                    string textLine = reader.ReadLine();

                    if (textLine == null)
                        break;

                    writer.WriteLine(textLine);
                }
            }

            if (File.Exists(_standardErrorFileName))
            {
                FileInfo info = new FileInfo(_standardErrorFileName);

                // if the error info is empty or just contains eof etc.

                if (info.Length < 4)
                    info.Delete();
            }
        }

        #endregion  // Private Methods

        #region Private Fields

        private StreamReader _standardError  ;
        private StreamReader _standardOutput ;
        private StreamWriter _standardInput  ;

        private string _standardInputFileName;
        private string _standardOutputFileName;
        private string _standardErrorFileName;

        ProcessStartInfo _processStartInfo = new ProcessStartInfo();


        #endregion  // Private Fields
    }
}
3
  • Hi @Matthew Watson , I looked into the class you wrote , One doubt i need to ask , Currently the Run() method waits for the thread to completed , It doesn't return the control back to the calling Thread (Main thread) . How that could be achieved .
    – coder3521
    Dec 24, 2015 at 6:25
  • On linux this is supported without using a shell (directly by the kernel). Ex here in C : stackoverflow.com/questions/2605130/… I don't know how it works on Windows
    – Kiruahxh
    Mar 18, 2021 at 16:33
  • The example link is broken
    – TaW
    Mar 31, 2022 at 10:34
9

This is an old question, but it came up on the first google link for searching for how to redirect the standard output of a launched Process to a file.

The answer I think works well is to add a OutputDataReceived event and write to the file there.

Here is a complete working example.

using System;
using System.IO;
using System.Diagnostics;
using System.Text;

class StandardAsyncOutputExample
{

    public static void Main()
    {
        var outputStream = new StreamWriter("output.txt");
        Process process = new Process();
        process.StartInfo.FileName = "ipconfig.exe";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
        {
            if (!String.IsNullOrEmpty(e.Data))
            {
                outputStream.WriteLine(e.Data);
            }
        });

        process.Start();
       
        process.BeginOutputReadLine();

        process.WaitForExit(); 
        process.Close();
       
        outputStream.Close();

        Console.WriteLine("\n\nPress any key to exit.");
        Console.ReadLine();
    }
}
4

It didn't work because > redirect was supported by cmd.exe shell. Try the following code instead.

Process pr2 = new Process();
pr2.StartInfo.FileName = @"c:\windows\system32\cmd.exe";
pr2.StartInfo.Arguments = @"/k \"show-snps -Clr -x 2 out.delta > out.snps\"";
pr2.Start();
pr2.WaitForExit();
2
  • 1
    If the Arguments string is declared as Verbatim (with the @ at the start), then you can't escape the double-quote with a slash. Instead put in two adjacent double-quotes to signify a single. The example above should be: @"/k ""show-snps -Clr -x 2 out.delta > out.snps""";
    – erict
    May 24, 2019 at 17:54
  • @erict; I'm having same problem. This solution is enough to run the process. But I want to know the error thrown in the cmd. I've tried to use pr2.StandardError.ReadToEnd().ToString().Split(new[] { ':' }) with RedirectStandardError = true; So, long there were no errors while executing. But it never stop to return something. So, i want to know, how do i read the errors after a command is executed, with cmd.exe /k ""some command here > output.file"". Please help. Jul 27, 2020 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.