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

时间:2014-01-09 22:02:10

标签: vb.net winforms pdf

我们有一个Win Forms应用程序,它使用iTextSharp生成pdf,将其保存到本地目录,然后应用程序打开该文件。有一个客户(所有XP盒和Adobe 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()

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

之前有人遇到过这种奇怪的事情吗?

编辑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

2 个答案:

答案 0 :(得分:12)

您正在阅读错误消息。我更加强调相关部分:

  

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

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

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

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

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

Acrobat context menu as seen in Windows Explorer

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

答案 1 :(得分:0)

当打开文件的默认行为与打开文件的相对行为之间存在差异时,实际发生此错误。 例如,如果您选择了默认应用程序来打开.pdf文件作为Internet Explorer,并且您尝试使用Process.Start()方法打开同一文件。您将收到异常,因为根据默认操作,它应该在Internet Explorer中打开该文件,并且您的应用程序正在尝试使用Adobe reader打开它。

要解决此问题,请将.pdf文件的默认应用程序设置为Adobe Reader,您不会再收到此错误。