Click here to Skip to main content
13,899,432 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


19 bookmarked
Posted 17 Jun 2008
Licenced CPOL

Redirecting Text Output to a File from a Console Application

, 17 Jun 2008
Rate this:
Please Sign up or sign in to vote.
A short article describing how to redirect text output from a console application to a file.


I decided to write this article after being unable to find any example code on how to redirect console output to a file. There were examples of writing to a memory stream but I needed to output to a file because I was worried about the potential size of the memory stream required to do what I wanted. I had much help from my friend Armand so I would like to have him credited as a major contributor.


I was experiencing errors when I used the redirection within the arguments sent to the process.

My code was written to back up a MySQL database using the tool mysqldump.exe but it should be easily changed to work with other console applications.

Using the Code

The sample is quite small. I have used comments to describe the process.

StreamWriter _outputStream;

// Called asynchronously with a line of data
private void OnDataReceived(object Sender, DataReceivedEventArgs e)
    if ((e.Data != null)&&(_outputStream != null))

public void BackupLocalDatabase(string DatabaseName, string UserID, string Password,
    string OutputFilename)
#region BackupLocalDatabase
    string fnName = "BackupLocalDatabase";
        _outputStream = new StreamWriter(OutputFilename);
            // --databases to force CREATE DATABASE
            // --hex-blob to force output of blobs as hex strings
            string _args = string.Format(
                " --databases {0} --user={1} --password={2} --hex-blob",
                DatabaseName, UserID, Password);

            // Info object
            ProcessStartInfo _info = new ProcessStartInfo("mysqldump");
            _info.Arguments = _args;
            _info.UseShellExecute = false;
            _info.RedirectStandardError = true;
            _info.RedirectStandardOutput = true;

            // Process object
            Process _process = new Process();
            _process.StartInfo = _info;

            // Set up the event handler to call back with
            // each line of output
            _process.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);

            //TODO error checks here

    catch (Exception e)
        LogException(fnName, e);
        throw e;
}// function


2008-06-16 Written


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionCould you please improve your article? Pin
drweb8623-Jun-08 1:11
memberdrweb8623-Jun-08 1:11 
GeneralInteresting Pin
merlin98118-Jun-08 3:50
professionalmerlin98118-Jun-08 3:50 
GeneralRe: Interesting Pin
drweb8623-Jun-08 1:12
memberdrweb8623-Jun-08 1:12 
GeneralRe: Interesting Pin
wizardzz11-Dec-12 11:00
memberwizardzz11-Dec-12 11:00 
QuestionWhat problems? Pin
PIEBALDconsult17-Jun-08 6:01
protectorPIEBALDconsult17-Jun-08 6:01 
QuestionAm I missing something obvious? Pin
Humble Programmer17-Jun-08 5:52
memberHumble Programmer17-Jun-08 5:52 
AnswerRe: Am I missing something obvious? Pin
drweb8623-Jun-08 1:08
memberdrweb8623-Jun-08 1:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 17 Jun 2008
Article Copyright 2008 by czeshirecat
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid