how to open selected files through the default application of the file in VB 2010?

前端 未结 2 1796
温柔的废话
温柔的废话 2021-01-19 02:16

I have Windows application written in VB 2010.Here, user may select any file from open dialog.So, I want to open the file in corresponding application.for example, suppose u

2条回答
  •  不思量自难忘°
    2021-01-19 02:55

    Try this:

    now with openfiledialog

    Dim OpenFileDlg as new OpenFileDialog.
    
    OpenFileDlg.FileName = "" ' Default file name
    OpenFileDlg.DefaultExt = ".xlsx" ' Default file extension
    OpenFileDlg.Filter = "Excel Documents (*.XLSX)|*.XLSX"
    OpenFileDlg.Multiselect = True
    OpenFileDlg.RestoreDirectory = True
    ' Show open file dialog box
    Dim result? As Boolean = OpenFileDlg.ShowDialog()
    
    ' Process open file dialog box results
    for each path in OpenFileDlg.Filenames
        Try
            System.Diagnostics.Process.Start(Path)
    
        Catch ex As Exception
            MsgBox("Unable to load the file. Maybe it was deleted?")
        End Try
        If result = True Then
            ' Open document
        Else
            Exit Sub
        End If
    next
    

    This will work if the file is registered with the OS. Use Try catch because it can throw errors if the file is in use.

    Edit: It uses always the default application.

提交回复
热议问题