Hack Forums
[.NET] Get default browser path - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Get default browser path (/showthread.php?tid=4952708)



Get default browser path - DarkMethods - Aug 17, 2015

I did some research around the internet on how to get default browser's path.
I found only long codes, sometimes needing whole modules and APIs; so I decided to write my own method, just few lines of code and no APIs needed...

Here it's code.
Code
Function DefaultBrowser() As String

Dim regshell As Object
Set regshell = CreateObject("Wscript.Shell")

' Reads the registry value where is stored the default application to use with HTTP
DefaultBrowser = regshell.regread("HKEY_CLASSES_ROOT\HTTP\shell\open\command\")

' Removes shell parameters after the actual path, preserving only path included in double       ' quotes
DefaultBrowser = Left(DefaultBrowser, InStr(1, DefaultBrowser, ".exe", vbTextCompare) + 4)

' Removes double quotes (")
DefaultBrowser = Replace(DefaultBrowser, Chr(34), vbNullString)

End Function



* - Aristocles - Aug 17, 2015

Quote: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.

Quote: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


http://stackoverflow.com/questions/18995086/finding-the-default-browser-in-vb-net