How to start an Adobe Reader or Acrobat from VB.NET?

扶醉桌前 提交于 2019-12-11 02:05:51

问题


From Windows Explorer, double-clicking a PDF opens the document in Adobe Reader. Perfect!

But PROCESS.START(pdfdocumentpath) in my Winforms application opens the PDF in IE. Is there a setting somewhere that will allow PROCESS.START (or other VB.NET code) to open the document in the same way as Windows Explorer?

Some of my users have 32-bit machines, some have 64-bit machines. Some have Adobe Reader, some have Adobe Acrobat. Some may be a version or more behind, some will be current. Some will have the products in their standard locations, some may have installed them elsewhere.

What I want to do is open the document in Adobe Reader if they have it and Adobe Acrobat if they have that.

How can I accomplish this?


回答1:


Use a try catch for it.
And you do not always need to provide a path ."Some programs you can start with just the name"

Adobe acrobat = acrobat
Acrobat reader = AcroRd32
Visual studio = devenv
And so on

Now to the code :)

First check if the file exists with If My.Computer.FileSystem.FileExists(FilePath) Then
If file exists you do the try."If not MsgBox("File not found.")"

So first try to open Adobe Acrobat "Process.Start("acrobat", FilePath)"
If that dos not work do another try in the catch.
So now try to open acrobat reader."Process.Start("AcroRd32", FilePath)"
Again if this dos not work use the catch do do another try.
But now just use "Process.Start(FilePath)".
So in the last catch you tell the user to instal acrobat reader. :)

Dim FilePath As String = "C:\Test.pdf"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    If My.Computer.FileSystem.FileExists(FilePath) Then
        Try
            Process.Start("acrobat", FilePath)
        Catch ex As Exception
            Try
                Process.Start("AcroRd32", FilePath)
            Catch ex2 As Exception
                Try
                    Process.Start(FilePath)
                Catch ex3 As Exception
                    MsgBox("Instal Acrobat Reader")
                End Try
            End Try
        End Try
    Else
        MsgBox("File not found.")
    End If

End Sub


来源:https://stackoverflow.com/questions/24874557/how-to-start-an-adobe-reader-or-acrobat-from-vb-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!