472,958 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Having problems running console command with parameters

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 { }

Jun 27 '08 #1
1 4730
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

Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hal Vaughan | last post by:
I know that I need to launch a VBS file on Windows this way: Win9x: Runtime.getRuntime().exec("start script.vbs"); WinNT: Runtime.getRuntime().exec("cmd /c start script.vbs"); I'm doing...
7
by: UJ | last post by:
I got help from somebody here telling me how to run a DOS command using the process variable. Problem for me is that I need to be able to pass parameters to the program - multiple parameters at...
1
by: Michael Hetrick | last post by:
How would I pass parameters to a console application? I would like to do something like this: consoleapp.exe /o \\fileshare\origindirectory /d \\fileshare\destinationdirectory I'm not sure...
7
by: Samantha Penhale | last post by:
Hello, Thanks in advance for any insight you can offer. I've a ASP.NET project written in C#, two web forms, a lovely gob of using statements. I originally had one webform with all my fields and...
3
by: Steve Le Monnier | last post by:
Quick question, I've designed an app to be run from the command window, and am trying to output a message to the command window if the incorrect number of parameters are passed. I've used...
17
by: Csaba Gabor | last post by:
Is there a way to determine the path to the php executable (as opposed to the script. In other words, I am looking for the path to php.exe or php-win.exe) that is currently running (ie. how was...
3
by: Nicol | last post by:
Hi, I have created a console application in c#.net. I also scheduled it in the windows scheduler. In the scheduled task i will specify the command line arguments. When I say "Run", I am getting...
3
by: Alex | last post by:
Hello, I'm wroting a console utility in VB 2005, and I need the ability to pass parameters to the application when the program runs. For example, if my program is called testing.exe, I need to...
1
by: =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?= | last post by:
On reflection, you could possibly make the app a self extracting zip file which extracts the EXE and a settings file and then starts the app, then when you app closes, it can repack the settings...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.