2011-10-27 52 views
2

我有一个windows .net程序(除其他外)将显示图像文件。这些文件可以是TIFF或PDF格式,目前显示的方式是查看文件扩展名,然后调用相应的程序来显示该文件。.net Windows应用程序 - 如何通过文件关联自动调用程序

下面的代码片段:

  imagepath = imagedataset.Tables("Table").Rows(imagecal).Item(2) 
     imagepath = "\\tylerimaging\DocumentUpload\" & imagedataset.Tables("Table").Rows(imagecal).Item(3) & "\" & imagedataset.Tables("table").Rows(imagecal).Item(4) 
     Dim PDFImage As String = imagepath.Substring(imagepath.Length - 3) 
     If UCase(PDFImage) = "PDF" Then 
      System.Diagnostics.Process.Start("AcroRd32.exe", imagepath) 
     Else 
      Try 
       System.Diagnostics.Process.Start("MSPVIEW.EXE", imagepath) 
      Catch ex As Exception 
       If ex.Message = "The system cannot find the file specified" Then 
        System.Diagnostics.Process.Start("ois.exe", imagepath) 
       End If 
      End Try 
     End If 
    End If 

现在的问题是,如果有人没有安装Acrobat Reader,例如,而是使用Adobe Acrobat的完整版,为的Process.Start AcroRd32 .exe将失败。但是,Windows显然具有PDF和Acrobat文件类型之间的关联 - 所以,这里是我的问题 - 如何通过与Windows中的该文件类型关联的任何程序显示文件?

在此先感谢....

+0

是相关http://stackoverflow.com/questions/258416/shellexecute-equivalent-in-net – Jeremy

回答

4

尝试调用的Process.Start上的PDF或TIFF文件本身。如果没有与文件类型相关联,Windows将处理它或引发异常。

+0

工作完美 - 谢谢。 –

2

Call Process.Start()只传递文档文件名。默认情况下,它使用UseShellExecute选项,这意味着要求shell在文档上执行开放式动词。这与从外壳UI双击文档相同。

0

我们已经实现了一个公用库方法集,它将首先尝试使用Process.Start打开文件,如果失败,则提示用户选择应用程序以打开文件(打开为)。

此实现还解决了打开RTF文件时会引发InvalidOperationException的遗留问题。通常的入口点是使用文件的完整路径调用OpenFileForUser。

Public Structure SHELLEXECUTEINFO 
    Public Size As Integer 
    Public Mask As Integer 
    Public hwnd As IntPtr 
    <MarshalAs(UnmanagedType.LPStr)> Public Verb As String 
    <MarshalAs(UnmanagedType.LPStr)> Public File As String 
    <MarshalAs(UnmanagedType.LPStr)> Public Parameters As String 
    <MarshalAs(UnmanagedType.LPStr)> Public Directory As String 
    Dim Show As Integer 
    Dim InstApp As IntPtr 
    Dim IDList As IntPtr 
    <MarshalAs(UnmanagedType.LPTStr)> Public [Class] As String 
    Public hkeyClass As IntPtr 
    Public HotKey As Integer 
    Public Icon As IntPtr 
    Public Process As IntPtr 
End Structure 

Public Const SW_NORMAL As Integer = 1 

' Code For OpenWithDialog Box 
<DllImport("Shell32", CharSet:=CharSet.Auto, SetLastError:=True)> _ 
Public Function ShellExecuteEx(ByRef lpExecInfo As SHELLEXECUTEINFO) As Boolean 
End Function 

''' <summary> 
''' This method executes the shell method for opening the specified file, allowing the user to choose 
''' which program they would like to use to open the file. 
''' </summary> 
''' <param name="sFileName"></param> 
''' <remarks></remarks> 
Public Sub OpenFileForUserAs(ByVal sFileName As String) 
    ' Exceptions are handled by the caller 

    Dim oShellExecuteInfo As New SHELLEXECUTEINFO 

    With oShellExecuteInfo 
     .Size = System.Runtime.InteropServices.Marshal.SizeOf(oShellExecuteInfo) 
     .Verb = "openas" 
     .File = sFileName 
     .Show = SW_NORMAL 
    End With 
    If Not ShellExecuteEx(oShellExecuteInfo) Then 
     Throw New System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error()) 
    End If 
End Sub 

''' <summary> 
''' This method opens the file for the user 
''' </summary> 
''' <param name="sFileName"></param> 
''' <remarks></remarks> 
Public Function OpenFileForUser(ByVal sFileName As String) As System.Diagnostics.Process 
    ' Exceptions are handled by the caller 

    Try 
     Return System.Diagnostics.Process.Start(sFileName, "") 
    Catch theInvalidOperation As InvalidOperationException 
     ' happens with rtf; just sink the exception 
    Catch ex As System.ComponentModel.Win32Exception 
     Select Case ex.NativeErrorCode 
      Case 1155 
       Call OpenFileForUserAs(sFileName) 
      Case 1223 
       ' Operation Cancelled By User 
      Case Else 
       Throw ex 
     End Select 
    End Try 

    Return Nothing 
End Function 
相关问题