首页 > 解决方案 > Process.start() 系统找不到指定的文件

问题描述

在将此问题标记为重复之前,请阅读它 - 我阅读了所有类似的问题,但我的问题不同。

运行一个进程,我得到了异常The System cannot find the file specified

此异常仅在重定向 StandardOutput 时发生。在代码的第 15 行,如果 Standardoutput 被重定向,我定义了一个决定。请在下面找到代码:

        Public Shared Function XPStoPDF(ByVal Input As String, ByVal Output As String, ByVal Executable As String) As String
        Dim StartupInfo As New System.Diagnostics.ProcessStartInfo()
        Dim p As System.Diagnostics.Process = Nothing

        If Not System.IO.File.Exists(Input) Then Throw New System.Exception("Error: Inputfile " & Input & " not found!")
        If Not System.IO.File.Exists(Executable) Then Throw New System.Exception("Error: GhostScriptfile " & Executable & " not found!")
        If Not System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(Output)) Then Throw New System.Exception("Error: Output path " & System.IO.Path.GetDirectoryName(Output) & " not found!")

        Try
            StartupInfo.FileName = System.IO.Path.GetFileName(Executable)
            StartupInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(Executable)
            StartupInfo.Arguments = " -sDEVICE=pdfwrite -sOutputFile=" & Output & " -dNOPAUSE " & Input

#Region "Switch for standardoutput export"
            If True Then 'Change to False and the code works!!!
#End Region
                StartupInfo.UseShellExecute = False
                StartupInfo.RedirectStandardOutput = True

                Dim CommandlineOutput As String = System.Diagnostics.Process.Start(StartupInfo).StandardOutput.ReadToEnd() '<---Exception thrown
                Return CommandlineOutput
            Else
                System.Diagnostics.Process.Start(StartupInfo)
                Return String.Empty
            End If
        Catch ex As System.Exception
            Throw New System.Exception("Error converting XPS File to PDF!" & System.Environment.NewLine & "Error details: " & ex.Message)
        End Try
    End Function

有人知道,标准输出内容的重定向有什么问题吗?

提前谢谢你,扬

标签: vb.netsystem.diagnosticsprocess.start

解决方案


好的,我找到了解决方案。我不知道为什么,但是在使用时UseShellExecute = False,必须为Filename 如果我使用定义完整的文件名和路径,则必须UseShellExecute = True将完整的文件名拆分为工作目录和文件名。


推荐阅读