2011-10-27 4 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 

、問題は誰かがアクロバットリーダーを持っていない場合、例えば、インストールされていることですが、Adobe Acrobatのフルバージョン、AcroRd32ためprocess.start:ここ

は、コードの断片です.exeは失敗します。しかし、Windowsは明らかにPDFのファイルタイプとAcrobatとの関連性を持っているので、ここで私の質問です。どのようなプログラムをWindowsでそのファイルタイプに関連付けて表示させるには?事前に

おかげで....

+0

が関連http://stackoverflow.com/questions/258416/shellexecute-equivalent-in-net – Jeremy

答えて

4

は、PDFまたはTIFFファイル自体にProcess.Startを呼び出す試してみてください。ファイルの種類に何も関連していない場合、Windowsはそれを処理したり、例外を発生させたりします。

+0

は完全に働いた - ありがとうございました。 –

2

ドキュメントファイル名だけを渡すProcess.Start()を呼び出します。デフォルトでは、これはUseShellExecuteオプションを使用します。つまり、シェルはドキュメント上でオープン動詞を実行するように求められます。シェルUIからドキュメントをダブルクリックするのと同じことです。

0

私たちは、Process.Startを使用してファイルを最初に開こうとする共通ライブラリセットを実装しました。失敗した場合は、アプリケーションを選択して(open as)でファイルを開くように求めます。

この実装では、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 
関連する問題