vb.net - 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
有人知道,标准输出内容的重定向有什么问题吗?
提前谢谢你,扬
解决方案
好的,我找到了解决方案。我不知道为什么,但是在使用时UseShellExecute = False
,必须为Filename
如果我使用定义完整的文件名和路径,则必须UseShellExecute = True
将完整的文件名拆分为工作目录和文件名。
推荐阅读
- scala - 水平展开或旋转 spark scala 数据框以创建大型平面数据框
- android - 在 recyclerView 中使用 ItemTouchHelper 时出现 java.lang.IndexOutOfBoundsException 错误?
- angular9 - 无法使用 Angular 9 在 ie9 中导出 xlsx 文件
- elasticsearch - 我如何通过在 Elasticsearch 中出现的频率来限制结果
- reactjs - 如何在反应组件中使用 window.onerror?
- c# - 提高大型项目 c# WPF XAML 中的性能
- mysql - 在分数表中获得排名
- python - IMAP4.search 按主题名称 python
- javascript - 如何映射 SetState 函数?
- javascript - 如何在 lightgallery 上添加自定义触发器?