2011-10-27 48 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 
相關問題