如何从 VB.NET 启动 Adob​​e Reader 或 Acrobat?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24874557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:53:12  来源:igfitidea点击:

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

vb.netpdfadobe

提问by Ira D

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

在 Windows 资源管理器中,双击 PDF 会在 Adob​​e Reader 中打开文档。完美的!

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?

但是我的 Winforms 应用程序中的 PROCESS.START(pdfdocumentpath) 在 IE 中打开 PDF。是否有某个设置允许 PROCESS.START(或其他 VB.NET 代码)以与 Windows 资源管理器相同的方式打开文档?

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.

我的一些用户有 32 位机器,一些有 64 位机器。有些有 Adob​​e Reader,有些有 Adob​​e Acrobat。有些可能是一个或更多的版本,有些将是最新的。有些将在其标准位置安装产品,有些可能已将其安装在其他地方。

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

我想要做的是在 Adob​​e Reader 中打开文档(如果有的话)和 Adob​​e Acrobat(如果有)。

How can I accomplish this?

我怎样才能做到这一点?

回答by Creator

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

Adobe acrobat = acrobat
Acrobat reader = AcroRd32
Visual Studio = devenv
等等

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.")"

首先检查文件是否存在 If My.Computer.FileSystem.FileExists(FilePath) Then
If file exists 你试试。"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. :)

所以首先尝试打开 Adob​​e Acrobat "Process.Start("acrobat", FilePath)"
如果这不起作用,请再试一次。
所以现在尝试打开 acrobat 阅读器。"Process.Start("AcroRd32", FilePath)"
如果这不起作用,请再次尝试使用 catch 再试一次。
但现在只需使用“Process.Start(FilePath)”。
因此,在最后一个 catch 中,您告诉用户安装 acrobat 阅读器。:)

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