2

I followed several hints on how to get the "default browser" in windows, but they all are how to read the registry path from Current.User: Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice

HOWEVER, I have both Chrome and Edge on my machine. When switching between the two as the default, I always get "ChromeHTML". So, this does not seem like it will work!

This happens to be for a .NET MAUI app

3
  • Are you referring to the ProgId value? What do you see under HKEY_CLASSES_ROOT\ChromeHTML ? Commented Sep 20 at 2:14
  • stackoverflow.com/a/76966324/403671 Commented Sep 20 at 7:00
  • @Dai, whether Chrome or Edge is set as default, this shows info about Chrome. Commented Sep 22 at 16:27

1 Answer 1

1

This works on Windows 11:

internal static bool TryGetSystemDefaultBrowserPath(out string path)
{
    path = string.Empty;

    try
    {
        using var regDefault = Registry.CurrentUser.OpenSubKey(
            name: "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.htm\\UserChoice",
            writable: false);

        if (regDefault?.GetValue("ProgId") is not string { Length: > 0 } stringDefault)
        {
            return false;
        }

        using var regKey = Registry.ClassesRoot.OpenSubKey(
            name: stringDefault + "\\shell\\open\\command",
            writable: false);

        if (regKey?.GetValue(null) is not string raw ||
            string.IsNullOrWhiteSpace(raw))
        {
            return false;
        }

        var name = raw.Trim().Trim('"');

        var exeIndex = name.IndexOf(".exe", StringComparison.OrdinalIgnoreCase);

        if (exeIndex < 0)
        {
            return false;
        }

        name = name[..(exeIndex + 4)];
        path = name;
        return true;
    }
    catch
    {
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

@bobwki You've set the bounty after I posted this answer. Does that mean that this answer didn't solve your issue?
well, on my system it always returned "ChromeHTML", whether set to Chrome or Edge. So, no. I appreciated your answer, though, so I upvoted.
This method returns the "path" to the default browser. Can you check again?
I tried again today, and, maddeningly, when I changed default browser to Edge, and restarted Windows, and verified by using the Windows Search box to open my website, your call still returns CHROME -- C:\Program Files\Google\Chrome\Application\chrome.exe. Hard to believe...
EVEN MORE MADDENING -- I uninstalled Chrome. Now your code returns "false". "stringDefault" is set to "ChromeHTML", which leads to the next OpenSubKey to return NULL.

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.