尝试重定向输出时 Process.Start 失败

发布于 2024-08-11 07:20:10 字数 1848 浏览 5 评论 0原文

我一直致力于自动化构建过程,并希望找到一种轻松的方法来定期运行单元测试。为此,我组装了一个简单的应用程序,用于检查项目文件并准备要测试的解决方案列表。该原型的工作原理是,测试执行时得到了预期的结果,但一旦我尝试重定向输出,应用程序就会在 Process.Start 调用上崩溃,抱怨找不到文件。

我已经对我在其他地方看到的所做的事情进行了多次迭代,包括这里的几篇文章,但我还没有让它正常工作。

这有效:

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

这不起作用:

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:系统找不到指定的文件 在 System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) 在 System.Diagnostics.Process.Start()... yadda yadda yadda”

任何使用此模型解决此问题的建议将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故笙诉离歌 2024-08-18 07:20:10

UseShellExecute 导致在不同的位置查找该文件。

来自 MSDN

WorkingDirectory 属性的行为不同当 UseShellExecute 为 true 时与 UseShellExecute 为 false 时相比。当 UseShellExecute 为 true 时,WorkingDirectory 属性指定可执行文件的位置。如果WorkingDirectory 是空字符串,则当前目录被理解为包含可执行文件。

当 UseShellExecute 为 false 时,WorkingDirectory 属性不用于查找可执行文件。相反,它由启动的进程使用,并且仅在新进程的上下文中才有意义。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文