Hi all!

I'm stuck somewhere with starting a process from vb.net.
I want to start a mysqldump process from my vb.net. This code works perfect if I run it from windows command prompt:

 "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" --user=root --password=mypassword --host=localhost --port=3306 --database sakila > "C:\backup\sakilabackup.sql"""

Then I took it to my VB App and put it in :

Process.Start ( "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" --user=root --password=mypassword --host=localhost --port=3306 --database sakila > "C:\backup\sakilalbackup.sql""")

But If I put this to VB, it will only ofcourse, read only to the second " (that's "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" )

So, I decided to break the code into blocks as follows:

        Dim stat As String
        Dim stat2 As String
        Dim stat3 As String
        Dim stat4 As String

        stat4 = """"
        stat = "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe"""
        stat2 = "--user=root --password=mkwatuke --host=localhost --port=3306 --database sakila >"
        stat3 = "C:\backup\shekelbackup.sql"""

Then I joined all these stats to produce one line of code by this:

txtPath.Text = stat4 & "" & stat & " " & stat2 & "" & stat4 & stat3 & stat4 & stat4 & "" (I have a textbox called txtPath)

On this txtPath textbox, the code looks similar to the one I executed with command prompt.

Then I did this:

Process.Start(txtPath.Text)

but, upon running this, I get the error "The system cannot find the file specified"
I even tried Process.Start (and put the all the 'stats' here), but I get the same problem.

I'm have verified that the same line of code is passed. (Otherwise, the code works in the command prompt)

What could be causing trouble? Any suggestions please. Any links to read more about the process.start?
Thanks.

Recommended Answers

All 6 Replies

Process.Start can take two arguments. The first is the executable, the second is the arguments.

System.Diagnostics.Process.Start(txtProgram.Text, txtArguments.Text)

For future reference, it is not appropriate to post in other threads in order to direct attention to your problem in another. It is especially inappropriate to hijack a dead thread to do that. You did both of these things by posting in the MS SQL thread. I deleted both posts (you posted the same redirect twice).

Reverend, I apologize for the violation. I've read the Daniweb rules again.(But I think there should be a way of asking the asker of a solved thread how he/she solved it if he did not say in the thread?, I understand we're not allowed to ask by email or private msg)

And, Thank you for your response, I tried with the code you gave me, but I still got "The system can not find the file specified" error msg. Yet, if I take the contents of (txtProgram.Text) and join with contents of (txtArguments.Text) in a command prompt, it works well.

Any help please?

Here is a sample of setting options. My example uses cmd.exe. You can substitute your own exec and args.

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim exec As String = "C:\Windows\System32\cmd.exe"
        Dim args As String = "/c dir d:\ftp"
        Dim info As New ProcessStartInfo(exec, args)

        info.UseShellExecute = False         'must be False to redirect
        info.RedirectStandardError = True
        info.RedirectStandardOutput = True
        'info.RedirectStandardInput = True   'not used in this example
        info.CreateNoWindow = True           'set to False if you want the window visible

        Dim proc As New Process()
        proc.StartInfo = info
        proc.Start()
        proc.WaitForExit()

        Dim out As String = proc.StandardOutput.ReadToEnd()
        Dim err As String = proc.StandardError.ReadToEnd()

        'At this point you have the Standard Output and Standard Error text in strings.
        'I'm writing them to the IDE Immediate window. You can write them to a text file.

        Debug.WriteLine(vbCrLf & vbCrLf & "STDOUT" & vbCrLf & vbCrLf & out)
        Debug.WriteLine(vbCrLf & vbCrLf & "STDERR" & vbCrLf & vbCrLf & err)

    End Sub

End Class

This has not been thoroughly tested so I don't know what will happen with a longer process with more lines of output but it is a starting point.

This one works much better

Public Class Form1

    Private WithEvents proc As Process

    Private Out As String = ""
    Private Err As String = ""

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        proc = New Process()

        With proc.StartInfo
            .FileName = "C:\Windows\System32\cmd.exe"
            .Arguments = "/c dir /s e:\series"
            .UseShellExecute = False
            .RedirectStandardError = True
            .RedirectStandardOutput = True
            .RedirectStandardInput = True
            .CreateNoWindow = True
        End With

        proc.Start()
        proc.BeginOutputReadLine()
        proc.BeginErrorReadLine()
        proc.WaitForExit()

        Debug.WriteLine(vbCrLf & vbCrLf & "STDOUT" & vbCrLf & vbCrLf & Out)
        Debug.WriteLine(vbCrLf & vbCrLf & "STDERR" & vbCrLf & vbCrLf & Err)

    End Sub

    Private Sub OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles proc.OutputDataReceived
        Out &= e.Data & vbCrLf
    End Sub

    Private Sub ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles proc.ErrorDataReceived
        Err &= e.Data & vbCrLf
    End Sub

End Class

Please note that if you want to write the output to a textbox (or any other) control instead of buffering it in a string you must use delegates (which I can explain if you need). If you have a lot of output then you should use StringBuilder rather than String.

Reverend, Everything you wrote in your reply is new to me!, So, first I want to thank you!

To begin with it, put your code into a form I created as it is, then to experiment with it, I assumed your code is supposed to open cmd.exe, and I think, open the directory "/c dir /s e:\series") so I changed "/c dir /s e:\series" to "/c dir /s e:\CCNA" (there is a folder in my E: named CCNA in my computer) I think it was supposed to open the directory e:/ccna, but nothing happens when I load the form.

Did I misunderstand something?

I hope you ain't fed up helping the youngman.

The output of that code won't show show in your app. It will be in the immediate window in Visual Studio. To see it in your form try something like this:

Textbox1.Text += vbCrLf & vbCrLf & "STDOUT" & vbCrLf & vbCrLf & Out
Textbox1.Text += vbCrLf & vbCrLf & "STDERR" & vbCrLf & vbCrLf & Err

Assuming, of course, that the Multiline property of the TextBox is set to True.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.