views:

289

answers:

1

I've been working on automating our build processes and wanted to come up with a painless way to run unit tests on a regular basis. To that end I've thrown together a simple app that examines the project files and prepares a list of solutions to test. The prototype works in that the tests are executed with the expected results, but as soon as I try to redirect my output the app bombs on the Process.Start call, complaining that a file could not be found.

I've tried several iterations on what I've seen done elsewhere, including several posts here, but I have yet to get this to work properly.

This works:

Private Function WTF(ByVal aWorkingDirectory As String, ByVal aFileName As String, ByVal aArguments As String) As Boolean

    Dim lProcess As New Process()
    With lProcess
        .StartInfo.WorkingDirectory = aWorkingDirectory
        .StartInfo.FileName = aFileName
        .StartInfo.Arguments = aArguments
    End With
    lProcess.Start()
    lProcess.WaitForExit()

End Function

This does not work:

Private Function WTF(ByVal aWorkingDirectory As String, ByVal aFileName As String, ByVal aArguments As String) As Boolean

    Dim lProcess As New Process()
    With lProcess
        .StartInfo.CreateNoWindow = True
        .StartInfo.UseShellExecute = False
        .StartInfo.RedirectStandardOutput = True
        .StartInfo.RedirectStandardError = True
        .StartInfo.WorkingDirectory = aWorkingDirectory
        .StartInfo.FileName = aFileName
        .StartInfo.Arguments = aArguments
    End With
    lProcess.EnableRaisingEvents = True 
    AddHandler lProcess.OutputDataReceived, AddressOf blah
    AddHandler lProcess.ErrorDataReceived, AddressOf blah
    lProcess.Start()
    lProcess.BeginOutputReadLine()
    lProcess.BeginErrorReadLine()
    lProcess.WaitForExit()

End Function

Private Shared Sub blah(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs)
    Console.WriteLine(e.Data)
End Sub

"System.ComponentModel.Win32Exception: The system cannot find the file specified at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start()... yadda yadda yadda"

Any suggestions for solving this using this model would be appreciated.

+1  A: 

UseShellExecute causes the file to be looked for in different places.

From MSDN:

The WorkingDirectory property behaves differently when UseShellExecute is true than when UseShellExecute is false. When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. If WorkingDirectory is an empty string, the current directory is understood to contain the executable.

When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used by the process that is started and has meaning only within the context of the new process.

xpda
Always the little things - thanks!
Jose Bueno