EBAG
EBAG

Reputation: 22561

Diagnostics.Process - Dump output to file

Hi I need to write the result of a mysqldump to a file with a standard windows commands.

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.WorkingDirectory = "sample directory";
proc.StartInfo.FileName = "mysqldump";
proc.StartInfo.Arguments = "-u root -pPassword --all-databases > db.sql";
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();

But it doesn't write to file this way... I don't want to read the output and then write it to file, since mysqldump output can become really big... Any solutions?

Upvotes: 3

Views: 2222

Answers (4)

Rahul Soni
Rahul Soni

Reputation: 4968

Pretty much on similar lines, but written in VB.NET. Convert it and you should be good... Link

Upvotes: 0

xcud
xcud

Reputation: 14722

Try executing through cmd.exe and enquote the command to keep your program from gobbling up the redirect:

proc.StartInfo.FileName = "cmd.exe";
proc.startinfo.Arguments = 
    "/c \"mysqldump -u root -pPassword --all-databases\" > db.sql"

Upvotes: 5

Jesper Fyhr Knudsen
Jesper Fyhr Knudsen

Reputation: 7927

If it's a lot of output you can use the proc.OutputDataReceived event, in the event handler just write the output to your file.

Read the MSDN article here

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062502

For piped output you may need ShellExecute to be true. If that doesnt work, you may had to pipe it yourself (i.e. either handle the data events, or have an async read/write loop), but the size shouldn't matter since you should only be reading small chunks of it at any time.

Upvotes: 0

Related Questions