没有应用程序与此操作的指定文件相关联 (VB.NET)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21032151/
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-09 17:22:53  来源:igfitidea点击:

No application is associated with the specified file for this operation (VB.NET)

vb.netwinformspdf

提问by gchq

We have a Win Forms application that produces a pdf with iTextSharp, saves it to a local directory and the application then opens the file. With one customer (all XP boxes and Adobe Reader 11) it throws the following error

我们有一个 Win Forms 应用程序,它使用 iTextSharp 生成 pdf,将其保存到本地目录,然后应用程序打开该文件。对于一位客户(所有 XP 框和 Adob​​e Reader 11),它会引发以下错误

No application is associated with the specified file for this operation
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()

Which would suggest Adobe Reader is not associated correctly with the pdf extension apart from the fact they can navigate to the local directory and open the file without any problem whatsoever.

这表明 Adob​​e Reader 与 pdf 扩展名没有正确关联,除了他们可以导航到本地目录并打开文件而没有任何问题。

Anyone come across this oddity before?

有没有人遇到过这种奇怪的事情?

Edit re ZippyV - example of a typical sub

编辑重新 ZippyV - 典型子示例

 Public Sub PDF_Functions_LogCalls_RunReport(ByVal Customer As Boolean)
    Try
        Dim vOutput As String = LogCalls_Run(Customer)
        If Left(vOutput, 5) = "Error" Then
            TaskDialog.Show(MainForm, AppBoxError("File Error", vOutput, "Error"))
            Exit Sub
        End If
        If System.IO.File.Exists(vOutput) Then
            Dim P As New Process
            P.StartInfo.FileName = vOutput
            P.StartInfo.Verb = "Open"
            P.Start()
        End If
    Catch ex As Exception
        EmailError(ex)
        Exit Sub
    End Try
End Sub

回答by Ken White

You're reading the error message wrong. I've added emphasis to the relevant part:

您正在阅读错误消息。我已经强调了相关部分:

No application is associated with the specified file for this operation

没有应用程序与此操作的指定文件相关联

This means that there is no application associated with the verb "Open". Change your code to simply use an empty string (or just don't set) the Verb:

这意味着没有与动词“打开”相关联的应用程序。更改您的代码以简单地使用空字符串(或只是不设置)Verb

P.StartInfo.FileName = vOutput
P.StartInfo.Verb = ""
P.Start()

This uses whatever the default operation is for the .pdf format, which will match the operation the user would get if they double-clicked the file in Windows Explorer.

这将使用 .pdf 格式的任何默认操作,这将与用户在 Windows 资源管理器中双击文件时获得的操作相匹配。

Recent versions of Acrobat are setting the default action to "Open with Adobe Reader XI" instead of just "Open", as you can see if you right-click a .pdf file.

最新版本的 Acrobat 将默认操作设置为“使用 Adob​​e Reader XI 打开”,而不仅仅是“打开”,您可以在右键单击 .pdf 文件时看到。

Acrobat context menu as seen in Windows Explorer

在 Windows 资源管理器中看到的 Acrobat 上下文菜单

This is seemingly what's causing the "not associated for this operation" error.

这似乎是导致“与此操作无关”错误的原因。

回答by the.net-learner

This error actually happens when there is a difference between the default behaviour of opening the file and the relative behaviour of opening the file. For example, if you have selected the default application to open .pdf files as Internet Explorer, and you are trying to open the same file using Process.Start() method. You will receive an exception because as per the default operations it should open that file in Internet Explorer and your application is trying to open it using Adobe reader.

当打开文件的默认行为与打开文件的相对行为之间存在差异时,实际上会发生此错误。例如,如果您已选择默认应用程序以 Internet Explorer 格式打开 .pdf 文件,并且您正尝试使用 Process.Start() 方法打开相同的文件。您将收到一个异常,因为按照默认操作,它应该在 Internet Explorer 中打开该文件,而您的应用程序正在尝试使用 Adob​​e Reader 打开它。

To rectify this set the default application for the .pdf file as Adobe Reader and you won't receive this error any more.

要纠正此问题,请将 .pdf 文件的默认应用程序设置为 Adob​​e Reader,您将不会再收到此错误。