gwilson
gwilson

Reputation: 73

Finding the default browser in VB.net

I am using Process.Start(url) to launch a URL in the default web browser and then I plan to close it using Process.Kill().

The problem is finding the default browser to know which process to kill. Suggestions?

Upvotes: 0

Views: 4554

Answers (1)

rcs
rcs

Reputation: 7197

Taken from: Opening default web browser

Private Function getDefaultBrowser() As String
    Dim browser As String = String.Empty
    Dim key As RegistryKey = Nothing
    Try
        key = Registry.ClassesRoot.OpenSubKey("HTTP\shell\open\command", False)

        'trim off quotes
        browser = key.GetValue(Nothing).ToString().ToLower().Replace("""", "")
        If Not browser.EndsWith("exe") Then
            'get rid of everything after the ".exe"
            browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4)
        End If
    Finally
        If key IsNot Nothing Then
            key.Close()
        End If
    End Try
    Return browser
End Function

There you can get the default browser. Then you can loop through the running process and kill the browser.

Dim browser As String
browser = getDefaultBrowser()
For Each p As Process In Process.GetProcesses        
    If p.ProcessName = browser Then
        p.Kill()
        Exit For
    End If
Next

Upvotes: 1

Related Questions