代码之家  ›  专栏  ›  技术社区  ›  Jan021981

进程.开始(系统找不到指定的文件)

  •  1
  • Jan021981  · 技术社区  · 7 年前

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

    The System cannot find the file specified

    此异常仅在重定向标准输出时发生。在代码的第15行中,如果标准输出被重定向,我定义了一个descion。请在下面找到代码:

            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
    

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jan021981    7 年前

    好吧,我找到了解决办法。 我不知道为什么,但是当使用 UseShellExecute = False Filename 如果我用 UseShellExecute = True