1

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
DiggDugg
  • 119
  • 8
  • If you try to put an http adress in your `Process.Start`, does it still open it with IE? – Antoine C. May 09 '18 at 12:17
  • No, it opens up in the default browser on my user account, which is Googles Chrome, as it should. This doesn't open my chosen homepage though, it just opens that URL. This action is different on my other user account on my laptop. There the default browser is Microsft Ege, and it opens the directed URL as it should, but again it is not the homepage. – DiggDugg May 09 '18 at 12:20
  • 1
    The default browser is the one that opens `.html` files. You can use [AssocQueryString](https://msdn.microsoft.com/en-us/library/windows/desktop/bb773471(v=vs.85).aspx) to find the opener. [Sample code here](https://stackoverflow.com/questions/47762031/start-process-a-file-without-extension-in-vb-net?answertab=active#tab-top). When you open the default browser by "name", it presents the HomePage. – Jimi May 09 '18 at 12:23
  • 1
    @Jimi, your solution seems to be the most elegant from what I have seen, but the 'AssocQueryStrin' appears to be a C++ command, I am using VB.Net. I have no idea about how to incorporate a C++ command into VB.Net. Will it work in VB? – DiggDugg May 09 '18 at 17:32
  • 1
    Where I wrote "Sample code here", that's a link to a VB.net sample code to implement this. It's mine, so if you have questions about it, no problem. – Jimi May 09 '18 at 17:37

3 Answers3

0

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)
Antoine C.
  • 3,730
  • 5
  • 32
  • 56
0

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) 
Andrew-UK
  • 1
  • 1
  • 2
-1

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.

Tejas
  • 1
  • 3
  • Whats the url of your homepage? – Tejas May 09 '18 at 12:50
  • OP said that it doesn't work with 'about:blank', this is the point of his question – Antoine C. May 09 '18 at 12:52
  • 1
    The URL to my desktop (Windows 7 Professional) homepage is a local page on my hard drive that I created and is [file:///C:/Startpage/default.htm] and its default browser is Google Chrome, but on my laptop the default browser is Microsoft Edge on Windows 10 and the homepage is set to [http://www.acer.msn.com](http://www.acer.msn.com). I can set the VB.Net project to open any webpage I want except apparently the actual homepage until I experiment with some of these answers and comments. – DiggDugg May 09 '18 at 16:49