none
How to Open a HTML file with a selected browser. RRS feed

  • Question

  • Can some one please help me.

    I have a HTML editor that works fine, except when I need to Open a selected HTML file with a selected browser, Ie: Edge, Chrome Etc.

    So far I have not been able to code it.

    Any help greatly appreciated.

    Thursday, November 16, 2017 11:52 PM

Answers

  •  Here is an example that gets the names and executable paths from the registry for any installed web browsers and adds them to a ComboBox as InstalledBrowser classes.  This is so you can select the browser from the combobox and press a button to open an html file or url address using the selected web browser.

     You will notice that i filter out any browsers other than Firefox,  Chrome,  and  IE.  This is because these three will open the url or file by just supplying the filename or url without using any command line switches.  I am not familiar with all the other odd-n-end browsers to know what command line switches they might need to use to specify the filename or url.  Some may work fine the way it is but,  some may need to have the arguments modified.

     You can try this in a new form with 1 ComboBox and 1 Button added to it.  You will also need to change the path to an html file to a file on your computer.

    Imports Microsoft.Win32
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox_InstalledWebBrowsers.DropDownStyle = ComboBoxStyle.DropDownList
            GetInstalledBrowsers()
        End Sub
    
        Private Sub GetInstalledBrowsers()
            Dim InstalledBrowsers As New List(Of InstalledBrowser)
            Dim validBrowsers() As String = {"chrome", "firefox", "iexplore"}
            Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Clients\StartMenuInternet", False)
            If rk IsNot Nothing Then
                For Each wbname As String In rk.GetSubKeyNames
                    Dim ib As New InstalledBrowser
                    Dim exePath As String = rk.OpenSubKey(wbname & "\shell\open\command").GetValue("").ToString.Replace(Chr(34), "")
                    If validBrowsers.Contains(IO.Path.GetFileNameWithoutExtension(exePath).ToLower) Then
                        ib.Name = IO.Path.GetFileNameWithoutExtension(exePath)
                        ib.ExePath = exePath
                        InstalledBrowsers.Add(ib)
                    End If
                Next
            End If
            ComboBox_InstalledWebBrowsers.DataSource = InstalledBrowsers
            ComboBox_InstalledWebBrowsers.DisplayMember = "Name"
            Button1.Enabled = (InstalledBrowsers.Count > 0)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim SelectedHtmlFile As String = Chr(34) & "C:\TestFolder\test.html" & Chr(34) 'you can select the file however you want, just make sure you add a Quote [Chr(34)="] to the start and end of it
            Dim ib As InstalledBrowser = CType(ComboBox_InstalledWebBrowsers.SelectedItem, InstalledBrowser)
            Process.Start(ib.ExePath, SelectedHtmlFile)
        End Sub
    End Class
    
    
    Public Class InstalledBrowser
        Public Property Name As String
        Public Property ExePath As String
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by mikki18 Friday, November 17, 2017 3:29 AM
    Friday, November 17, 2017 2:16 AM
  • Although I'm late to the party I'd like to offer another alternative.  The following uses the Windows shell to do all the work.  It is not necessary to know the location of executables or the details of command line parameters and no specific knowledge of the registry is needed.  And, as a bonus, it should work for any file type that is registered with the shell, not just html files.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Public Enum OPEN_AS_INFO_FLAGS
            OAIF_ALLOW_REGISTRATION = &H1 'enable the "always use this file" checkbox (NOTE If you don't pass this, it will be disabled)
            OAIF_REGISTER_EXT = &H2 'Do the registration after the user hits "ok"
            OAIF_EXEC = &H4 ' execute file after registering
            OAIF_FORCE_REGISTRATION = &H8 'force the "always use this file" checkbox To be checked (normally, you won't use the OAIF_ALLOW_REGISTRATION when you pass this)
            OAIF_HIDE_REGISTRATION = &H20 'hide the "always use this file" checkbox
            OAIF_URL_PROTOCOL = &H40 'the "extension" passed Is actually a protocol (uri scheme), And open With should show apps registered As capable Of handling that protocol
            OAIF_FILE_IS_URI = &H80 'pcszFile Is actually a URI
        End Enum
    
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
        Public Structure OPENASINFO
            <MarshalAs(UnmanagedType.LPWStr)>
            Dim strName As String
            <MarshalAs(UnmanagedType.LPWStr)>
            Dim strClass As String
            <MarshalAs(UnmanagedType.I4)>
            Dim oainfo As OPEN_AS_INFO_FLAGS
        End Structure
    
        <DllImport("Shell32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Unicode, PreserveSig:=False)>
        Public Shared Sub SHOpenWithDialog(ByVal hwnd As IntPtr, ByRef openasinfo As OPENASINFO)
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim oif As OPENASINFO
    
            oif.strName = "C:\Users\RLWA32\Documents\hw.html"
            oif.strClass = vbNullString
            oif.oainfo = OPEN_AS_INFO_FLAGS.OAIF_HIDE_REGISTRATION Or OPEN_AS_INFO_FLAGS.OAIF_EXEC
    
            Try
                SHOpenWithDialog(Handle, oif)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    End Class
    

    • Marked as answer by mikki18 Friday, November 17, 2017 6:51 PM
    Friday, November 17, 2017 3:14 PM
  • Use a class such as this one. Create an instance of the class, call the Open method with the executable name e.g. Chrome.exe, FireFox.exe and more likely will need the browser path too. All can be found in the Windows registry.

    Public Class WebBrowserHelper
        Public Property HasException As Boolean
        Private mException As Exception
        Public ReadOnly Property Exception As Exception
            Get
                Return mException
            End Get
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="pBrowserExecutableName">Browser executable</param>
        ''' <param name="pLink">Web page to open</param>
        ''' <returns>
        ''' True if no exception, false if an exeption was thrown. Note you could
        ''' simple use the last catch but this give you options.
        ''' 
        ''' If the function returns false and you check HasException and is true
        ''' then you can check the exception via the Exception property.
        ''' </returns>
        Public Function Open(ByVal pBrowserExecutableName As String, ByVal pLink As String) As Boolean
            Try
                Process.Start(pBrowserExecutableName, pLink)
            Catch noBrowser As ComponentModel.Win32Exception
                HasException = True
                If noBrowser.ErrorCode = -2147467259 Then
                    mException = noBrowser
                End If
            Catch other As Exception
                HasException = True
                mException = other
            End Try
    
            Return Not HasException
        End Function
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, November 17, 2017 12:34 AM
    Moderator

All replies

  • Use a class such as this one. Create an instance of the class, call the Open method with the executable name e.g. Chrome.exe, FireFox.exe and more likely will need the browser path too. All can be found in the Windows registry.

    Public Class WebBrowserHelper
        Public Property HasException As Boolean
        Private mException As Exception
        Public ReadOnly Property Exception As Exception
            Get
                Return mException
            End Get
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="pBrowserExecutableName">Browser executable</param>
        ''' <param name="pLink">Web page to open</param>
        ''' <returns>
        ''' True if no exception, false if an exeption was thrown. Note you could
        ''' simple use the last catch but this give you options.
        ''' 
        ''' If the function returns false and you check HasException and is true
        ''' then you can check the exception via the Exception property.
        ''' </returns>
        Public Function Open(ByVal pBrowserExecutableName As String, ByVal pLink As String) As Boolean
            Try
                Process.Start(pBrowserExecutableName, pLink)
            Catch noBrowser As ComponentModel.Win32Exception
                HasException = True
                If noBrowser.ErrorCode = -2147467259 Then
                    mException = noBrowser
                End If
            Catch other As Exception
                HasException = True
                mException = other
            End Try
    
            Return Not HasException
        End Function
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, November 17, 2017 12:34 AM
    Moderator
  •  Here is an example that gets the names and executable paths from the registry for any installed web browsers and adds them to a ComboBox as InstalledBrowser classes.  This is so you can select the browser from the combobox and press a button to open an html file or url address using the selected web browser.

     You will notice that i filter out any browsers other than Firefox,  Chrome,  and  IE.  This is because these three will open the url or file by just supplying the filename or url without using any command line switches.  I am not familiar with all the other odd-n-end browsers to know what command line switches they might need to use to specify the filename or url.  Some may work fine the way it is but,  some may need to have the arguments modified.

     You can try this in a new form with 1 ComboBox and 1 Button added to it.  You will also need to change the path to an html file to a file on your computer.

    Imports Microsoft.Win32
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox_InstalledWebBrowsers.DropDownStyle = ComboBoxStyle.DropDownList
            GetInstalledBrowsers()
        End Sub
    
        Private Sub GetInstalledBrowsers()
            Dim InstalledBrowsers As New List(Of InstalledBrowser)
            Dim validBrowsers() As String = {"chrome", "firefox", "iexplore"}
            Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Clients\StartMenuInternet", False)
            If rk IsNot Nothing Then
                For Each wbname As String In rk.GetSubKeyNames
                    Dim ib As New InstalledBrowser
                    Dim exePath As String = rk.OpenSubKey(wbname & "\shell\open\command").GetValue("").ToString.Replace(Chr(34), "")
                    If validBrowsers.Contains(IO.Path.GetFileNameWithoutExtension(exePath).ToLower) Then
                        ib.Name = IO.Path.GetFileNameWithoutExtension(exePath)
                        ib.ExePath = exePath
                        InstalledBrowsers.Add(ib)
                    End If
                Next
            End If
            ComboBox_InstalledWebBrowsers.DataSource = InstalledBrowsers
            ComboBox_InstalledWebBrowsers.DisplayMember = "Name"
            Button1.Enabled = (InstalledBrowsers.Count > 0)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim SelectedHtmlFile As String = Chr(34) & "C:\TestFolder\test.html" & Chr(34) 'you can select the file however you want, just make sure you add a Quote [Chr(34)="] to the start and end of it
            Dim ib As InstalledBrowser = CType(ComboBox_InstalledWebBrowsers.SelectedItem, InstalledBrowser)
            Process.Start(ib.ExePath, SelectedHtmlFile)
        End Sub
    End Class
    
    
    Public Class InstalledBrowser
        Public Property Name As String
        Public Property ExePath As String
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by mikki18 Friday, November 17, 2017 3:29 AM
    Friday, November 17, 2017 2:16 AM
  • Much thanks Kareninstructor and IronRazerz both answers work fine.

    Friday, November 17, 2017 3:28 AM
  • Although I'm late to the party I'd like to offer another alternative.  The following uses the Windows shell to do all the work.  It is not necessary to know the location of executables or the details of command line parameters and no specific knowledge of the registry is needed.  And, as a bonus, it should work for any file type that is registered with the shell, not just html files.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Public Enum OPEN_AS_INFO_FLAGS
            OAIF_ALLOW_REGISTRATION = &H1 'enable the "always use this file" checkbox (NOTE If you don't pass this, it will be disabled)
            OAIF_REGISTER_EXT = &H2 'Do the registration after the user hits "ok"
            OAIF_EXEC = &H4 ' execute file after registering
            OAIF_FORCE_REGISTRATION = &H8 'force the "always use this file" checkbox To be checked (normally, you won't use the OAIF_ALLOW_REGISTRATION when you pass this)
            OAIF_HIDE_REGISTRATION = &H20 'hide the "always use this file" checkbox
            OAIF_URL_PROTOCOL = &H40 'the "extension" passed Is actually a protocol (uri scheme), And open With should show apps registered As capable Of handling that protocol
            OAIF_FILE_IS_URI = &H80 'pcszFile Is actually a URI
        End Enum
    
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
        Public Structure OPENASINFO
            <MarshalAs(UnmanagedType.LPWStr)>
            Dim strName As String
            <MarshalAs(UnmanagedType.LPWStr)>
            Dim strClass As String
            <MarshalAs(UnmanagedType.I4)>
            Dim oainfo As OPEN_AS_INFO_FLAGS
        End Structure
    
        <DllImport("Shell32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Unicode, PreserveSig:=False)>
        Public Shared Sub SHOpenWithDialog(ByVal hwnd As IntPtr, ByRef openasinfo As OPENASINFO)
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim oif As OPENASINFO
    
            oif.strName = "C:\Users\RLWA32\Documents\hw.html"
            oif.strClass = vbNullString
            oif.oainfo = OPEN_AS_INFO_FLAGS.OAIF_HIDE_REGISTRATION Or OPEN_AS_INFO_FLAGS.OAIF_EXEC
    
            Try
                SHOpenWithDialog(Handle, oif)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    End Class
    

    • Marked as answer by mikki18 Friday, November 17, 2017 6:51 PM
    Friday, November 17, 2017 3:14 PM
  • Although I'm late to the party I'd like to offer another alternative.  The following uses the Windows shell to do all the work.  It is not necessary to know the location of executables or the details of command line parameters and no specific knowledge of the registry is needed.  And, as a bonus, it should work for any file type that is registered with the shell, not just html files.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Public Enum OPEN_AS_INFO_FLAGS
            OAIF_ALLOW_REGISTRATION = &H1 'enable the "always use this file" checkbox (NOTE If you don't pass this, it will be disabled)
            OAIF_REGISTER_EXT = &H2 'Do the registration after the user hits "ok"
            OAIF_EXEC = &H4 ' execute file after registering
            OAIF_FORCE_REGISTRATION = &H8 'force the "always use this file" checkbox To be checked (normally, you won't use the OAIF_ALLOW_REGISTRATION when you pass this)
            OAIF_HIDE_REGISTRATION = &H20 'hide the "always use this file" checkbox
            OAIF_URL_PROTOCOL = &H40 'the "extension" passed Is actually a protocol (uri scheme), And open With should show apps registered As capable Of handling that protocol
            OAIF_FILE_IS_URI = &H80 'pcszFile Is actually a URI
        End Enum
    
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
        Public Structure OPENASINFO
            <MarshalAs(UnmanagedType.LPWStr)>
            Dim strName As String
            <MarshalAs(UnmanagedType.LPWStr)>
            Dim strClass As String
            <MarshalAs(UnmanagedType.I4)>
            Dim oainfo As OPEN_AS_INFO_FLAGS
        End Structure
    
        <DllImport("Shell32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Unicode, PreserveSig:=False)>
        Public Shared Sub SHOpenWithDialog(ByVal hwnd As IntPtr, ByRef openasinfo As OPENASINFO)
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim oif As OPENASINFO
    
            oif.strName = "C:\Users\RLWA32\Documents\hw.html"
            oif.strClass = vbNullString
            oif.oainfo = OPEN_AS_INFO_FLAGS.OAIF_HIDE_REGISTRATION Or OPEN_AS_INFO_FLAGS.OAIF_EXEC
    
            Try
                SHOpenWithDialog(Handle, oif)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    End Class


    I am missing something.  If I have four browsers installed which browser will open the HTML document?  What if today I want to use Edge, and tomorrow I want to use Chrome?  How does this code accomplish that?

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Friday, November 17, 2017 3:32 PM
  • I am missing something.  If I have four browsers installed which browser will open the HTML document?  What if today I want to use Edge, and tomorrow I want to use Chrome?  How does this code accomplish that?

    It will cause the Windows Shell to display the "Open With" dialog on the screen.  You can then select the browser that you wish to use.  After you click on your selected browser the shell will open the document in the that  browser.
    Friday, November 17, 2017 3:41 PM
  • I am missing something.  If I have four browsers installed which browser will open the HTML document?  What if today I want to use Edge, and tomorrow I want to use Chrome?  How does this code accomplish that?

    It will cause the Windows Shell to display the "Open With" dialog on the screen.  You can then select the browser that you wish to use.  After you click on your selected browser the shell will open the document in the that  browser.

     OIC

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Friday, November 17, 2017 3:53 PM
  • Thanks for that RLWA32. It works good as well, now I will have to think over which solution to use.

    Great.!

    Friday, November 17, 2017 6:53 PM