Having problems running console command with parameters

C

Claire

Hi,
I'm trying to run mysqldump.exe from my c# application, running on windows
vista with visual studio 2005.
The following code does not produce the output file. The same line used in a
command prompt works fine.
While debugging, I get an exception "InvalidOperationException, StandardOut
has not been redirected or the process hasn't started yet." when attempting
the System.Console.Write(p.StandardOutput.ReadToEnd() line.
I get no exception but an output line of "mysqldump: Got errno 9 on write"
when attempting the System.Console.Write(p.StandardError.ReadToEnd())
command.
If I use a largish database and dont turn on the redirections, I appear to
get the output to the console rather than to file, but the text flashes by
so fast I can't read what's on the first few lines.
I don't know if it's something Im doing wrong, or vista. Is anyone able to
help please.

mysqldumpstring = @"--user=root --password=masterkey --hex-blob --databases
test > c:\temp\claire_dump.sql";

Process MySQLDump = new Process();
ProcessStartInfo info = new ProcessStartInfo("mysqldump.exe");// mysqldump
is in environment path
info.Arguments = mysqldumpstring;
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardError = true;

Process p = Process.Start(info);
p.WaitForExit();

try
{
System.Console.Write(p.StandardOutput.ReadToEnd());
}
catch { }
try
{
System.Console.Write(p.StandardError.ReadToEnd());
}
catch { }
 
C

Claire

The problem I had was caused by mysqldump getting confused by the section of
the argument string sending redirection of standard input to a file. This
meant that I needed to remove the output file name from the argument list.
As my database can become huge, I wanted to redirect standard output to file
rather than using the default memory stream.
My clever friend helped me with the following code showing redirection to
file.

StreamWriter OutputStream;

private void OnDataReceived(object Sender, DataReceivedEventArgs e)
{
// Write each line to the output file
if (e.Data != null)
OutputStream.WriteLine(e.Data);
}

public void BackupLocalDatabase(DatabaseHandler Handler, string
OutputFilename)
{
string fnName = "BackupLocalDatabase";
try
{
OutputStream= new StreamWriter(OutputFilename);
try
{
// Need to use "--databases" to force CREATE DATABASE command in
script
// --hex-blob to turn blob fields into hex strings
string mysqldumpstring = string.Format(" --databases
{0} --user={1} --password={2} --hex-blob",
Handler.DatabaseName, Handler.UserID, Handler.Password);

// Create info needed by process
ProcessStartInfo info = new ProcessStartInfo("mysqldump");
info.Arguments = mysqldumpstring;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;

// Create process
Process p = new Process();
p.StartInfo = info;
// Set up asynchronous read event
p.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();

//TODO, check for errors

}
finally
{
// Flush and close file
OutputStream.Flush();
OutputStream.Close();
}
}
catch (Exception e)
{
LogException(fnName, e);
throw e;
}

}// function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top