Jan021981
Jan021981

Reputation: 553

Process.start() System cannot find the file specified

before marking this question as a duplicate, please read it - I read all the similar questions, but my issue is different.

Running a process, I get the exception The System cannot find the file specified

This exception only occures, when redirecting the StandardOutput. In line 15 of the code, I defined a desicion if the Standardoutput is redirected. Please find the code below:

        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

Does anybody has an idea, whats wrong with the redirection of the standardoutput content?

Thank you in advance, Jan

Upvotes: 1

Views: 686

Answers (1)

Jan021981
Jan021981

Reputation: 553

Ok, I found the solution. I don't know why, but when using UseShellExecute = False, the full filename with path has to be defined for Filename If I use UseShellExecute = True, the full filename has to be splitted to the workingdirectory and the filename.

Upvotes: 1

Related Questions