VB不会显示PDF阅读器

时间:2015-02-03 10:23:23

标签: vb.net pdf process

我使用以下代码打开PDF文件:

Public Sub Execute_Doc(afilename As String, Optional style As ProcessWindowStyle = ProcessWindowStyle.Minimized)
        Dim myProcess As New Process

        Const ERROR_FILE_NOT_FOUND As Integer = 2
        Const ERROR_ACCESS_DENIED As Integer = 5


        Try
            myProcess.StartInfo.FileName = afilename
            myProcess.StartInfo.WindowStyle = style
            myProcess.Start()
        Catch e As System.ComponentModel.Win32Exception
            If e.NativeErrorCode = ERROR_FILE_NOT_FOUND Then
                Console.WriteLine(e.Message + ". Check the path.")
                MsgBox("File<" + afilename + "> not found!")
            Else
                If e.NativeErrorCode = ERROR_ACCESS_DENIED Then
                    Console.WriteLine(e.Message + ". You do not have permission to print this file.")
                    MsgBox("File <" + afilename + "> couldn't be opened!")
                End If
            End If
            MsgBox(e.ToString())
        Catch ex As Exception
            MsgBox(e.ToString())
        Finally
            myProcess.Kill()
            myProcess.Dispose()
        End Try
    End Sub

我调用Execute_Doc(“C:\ ProgrammName \ Test.pdf”,ProcessWindowStyle.Normal),但Adobe Reader不会显示。我可以在任务管理器中看到它。

如果我通过单击桌面上的默认图标首先启动Adobe Reader而没有任何文件,那么它是否有效。它也适用于Windows 8.1的集成PDF阅读器。我无法在Windows 7 / VS 2013计算机上调试此问题。这个问题只存在于ONE!客户端计算机。

任何tipps如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

Public Sub Execute_Doc(afilename As String, Optional style As ProcessWindowStyle = ProcessWindowStyle.Minimized)
    Dim myProcess As New Process

    Const ERROR_FILE_NOT_FOUND As Integer = 2
    Const ERROR_ACCESS_DENIED As Integer = 5


    Try
        myProcess.StartInfo.FileName = "AcroRd32.exe " & afilename
        myProcess.StartInfo.WindowStyle = style
        myProcess.Start()
    Catch e As System.ComponentModel.Win32Exception
        If e.NativeErrorCode = ERROR_FILE_NOT_FOUND Then
            Console.WriteLine(e.Message + ". Check the path.")
            MsgBox("File<" + afilename + "> not found!")
        Else
            If e.NativeErrorCode = ERROR_ACCESS_DENIED Then
                Console.WriteLine(e.Message + ". You do not have permission to print this file.")
                MsgBox("File <" + afilename + "> couldn't be opened!")
            End If
        End If
        MsgBox(e.ToString())
    Catch ex As Exception
        MsgBox(e.ToString())
    Finally
        myProcess.Kill()
        myProcess.Dispose()
    End Try
End Sub

答案 1 :(得分:0)

只需使用Shell Execute它由Win API提供,因此您不必担心安装了哪个程序来处理扩展。 Windows为您完成工作。

Private Function ShellExecute(ByVal File As String) As Boolean
   Dim myProcess As New Process
   myProcess.StartInfo.FileName = File
   myProcess.StartInfo.UseShellExecute = True
   myProcess.StartInfo.RedirectStandardOutput = False
   myProcess.Start()
   myProcess.Dispose()
End Function