473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 4778
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.