简体   繁体   中英

How do I open a user's default browser open to the users homepage?

I am trying to figure out how to cause a Menu Strip item to open the active Windows accounts default browser to their homepage. I have tried Process.Start("about:blank") and for some reason this always opens Internet Explorer's about:blank page. (I have Google Chrome as my default browser with http://www.duckduckgo.com as its homepage on Windows 7 Pro.)

I know I can specify any URL to open the default browser, but how to get their selected homepage to open? I have found some articles based in C# that required looking into registry entries as to finding their chosen homepage per each browser. Would the process be the same/similar in VB.Net 2017 and how would I go about doing so? This is using VB.Net 2017 Community Edition and the project is a Windows.Forms desktop application.

The only way I found is to manually query the registry about the default command to handle the http protocol.

The first line of this code will return something like "C:\Program Files\Your Browser\browser.exe" -osint -url "%1" , so you want to replace %1 by your landing page.

Then, if you want to use Process.Start with command line arguments, the first parameter will be the command and the second one the arguments. Thus, we need to split the registry string between the command and the argument list. The regex will do this job .

I omited null checks and regex success for clarity.

Dim cmd = CStr(Registry.ClassesRoot.OpenSubKey("http\shell\open\command").GetValue(String.Empty))
cmd = cmd.Replace("%1","about:blank")
Dim r = new Regex("^""([^""]+)"" (.*)")
Dim m = r.Match(cmd)
Process.Start(m.Groups(1).Value, m.Groups(2).Value)

Found some clues here .

Dim readValue As String = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\
Associations\UrlAssociations\http\UserChoice", "Progid", Nothing).ToString

Will give an identifier for the current user's browser.

Dim path As String = My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\"
& readValue & "\shell\open\command", "", Nothing).ToString

Will return the run command with path.

Add some code to extract the EXE and run it without arguments, for example;

 Dim DivArr As Char() = {Chr(34), "\"c}
'split into segments using quotes and back-slash seperators
 Dim parts() As String = path.Split(DivArr)
 'find first segment with period/full-stop
 Dim Executable As String = Array.Find(parts, Function(x) (x.Contains(".")))

Process.start(Executable) 

You may try this:

Process.Start("your_url_here eg. www.homepage.com etc.")

and, this will open with google chrome if its your default browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM