在将此问题标记为重复之前,请先阅读-我阅读了所有类似的问题,但是我的问题有所不同。
运行一个进程,我得到一个异常The System cannot find the file specified
仅在重定向StandardOutput时发生此异常。在代码的第15行中,我定义了标准输出重定向的目标。请在下面找到代码:
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
有人有想法吗,标准输出内容的重定向出了什么问题?
预先感谢您, 扬
答案 0 :(得分:1)
好的,我找到了解决方案。
我不知道为什么,但是在使用UseShellExecute = False
时,必须为Filename
定义带有路径的完整文件名
如果我使用UseShellExecute = True
,则必须将完整文件名拆分为工作目录和文件名。