0

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

For the kill to work I had to strip "browser" down with: browserp = IO.Path.GetFileName(browser) strfile = browserp Dim strSplit() As String strSplit = Split(strFile, ".") browserkill = (strSplit(0)) Thin replace browser with broweserkill. I'm sure there is a more elegant way but it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.