System.Diagnostics.Process.Start

cpopham

Well-known member
Joined
Feb 18, 2004
Messages
273
I am trying to use a process start to do a mysql dump. The -u and -p are swicthes that tell the executable what to do. I see the dos window open and close and no errors, but the dump file c:\test.sql is not created.

System.Diagnostics.Process.Start("C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump.exe", "-u root -pchecli02 myacct > c:\test23.sql")

Any ideas?

Thanks
Chester
 
C#:
ProcessStartInfo ps = new ProcessStartInfo(@"C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump.exe",@"-u root -pchecli02 myacct");
ps.UseShellExecute = false;
ps.RedirectStandardOutput = true;
Process p=new Process();
p.StartInfo = ps;
p.Start();
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"c:\test23.sql"))
 sw.Write(p.StandardOutput.ReadToEnd());
p.WaitForExit();
 
Thanks Joe Mamma,

That did the trick.....

Why do you have to use a streamwriter though for the output?

Thanks, Chester
 
Now how can I reverse this process to read the file backup? I have tried the following code without success:

Code:
        ps.UseShellExecute = False
        ps.RedirectStandardOutput = True
        Dim p As Process
        p = New Process
        p.StartInfo = ps
        p.Start()
        Dim sw As New System.IO.StreamReader("C:\testNew.sql")
        sw.Read(p.StandardInput)
        p.WaitForExit()

Thank You..

Chester
 
first. . . the streamwriter worked? I just hacked that out of the MSDN help.

the reason your first example failed is that the

" > testNew.sql "

part of the command line are not arguments but output redirection.

Now what to you mean reverse? Can you post the command line you want to emulate including any I/O redirections?
 
Last edited by a moderator:
and answering your question. . .

wouldnt the reverse actually be:

Dim sw As New System.IO.StreamReader("C:\testNew.sql")
p.StandardInput.Write(sw.ReadToEnd())

StandardOutput is a streamreader - only has read methods
StandardInput is a streamwriter - has write methods.

Conceptually you want to Write from a file to the process StandardInput and Read the process StandardOutput into a file.

make sense?
 
oh yeah - In VB.NOT remember to close your stream readers/writers to free resources.

In C# I wrap in a using statement to automatically close.
 
What my overall goal is is to use the builtin mysqldump application to backup a mysql database and then should something happen, I want to be able to restore from the backup that was generated. Yes your sample worked very well.

I tried using the streamreader to reverse the process. Using mysqldump, You would use a statement like:

mysqldump -u myuser -pmypassword mydatabasetorestoreto < mybackup.sql

The only difference in the statements in mysql is the greater than or less than sign..

Thanks, Chester
 
then I think it is:
Code:
[color=black]dim ps = as ProcessStartInfo = new ProcessStartInfo(@"C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump.exemysqldump -u myuser -pmypassword mydatabasetorestoreto")[/color]
ps.UseShellExecute = False
ps.RedirectStandardOutput = True
Dim p As Process
p = New Process
p.StartInfo = ps
p.Start()
Dim sr As New System.IO.StreamReader("C:\testNew.sql")
p.StandardInput.Write(sr.ReadToEnd())
p.WaitForExit()
 remember to close your streamreader
sr.Close()

again. . . I am thinking that will work. let me know!
 
oops!!!
Code:
[color=black]dim ps = as ProcessStartInfo = new ProcessStartInfo(@"C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump.exemysqldump -u myuser -pmypassword mydatabasetorestoreto")[/color]
ps.UseShellExecute = False
[b]ps.RedirectStandardInput = True[/b]
Dim p As Process
p = New Process
p.StartInfo = ps
p.Start()
Dim sr As New System.IO.StreamReader("C:\testNew.sql")
p.StandardInput.Write(sr.ReadToEnd())
p.WaitForExit()
 remember to close your streamreader
sr.Close()
 
Back
Top