how do i change default browser using c# or batch file

后端 未结 3 1230
小蘑菇
小蘑菇 2020-12-01 22:35

title speaks it all.

相关标签:
3条回答
  • 2020-12-01 22:49

    for the windows 7 pc you need to change registry key for the

    HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\ Associations\UrlAssociations\http
    

    you can change that using c#

    RegistryKey regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\shell\\Associations\\UrlAssociations\\http\\UserChoice", true);
    string browser = regkey.GetValue("Progid").ToString();
    
    if (browser != "IE.HTTP")
     {
          regkey.SetValue("Progid", "IE.HTTP");
     }
    

    for prior to vista os - (checked in windows XP)

    RegistryKey regkey = Registry.ClassesRoot.OpenSubKey("http\\shell\\open\\command", true);           
    string browser = regkey.GetValue(null).ToString().ToLower().Replace("\"", "");
    string defBrowser = "";
    if (!browser.EndsWith("exe"))
    {
            //get rid of everything after the ".exe"
            browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
           defBrowser = browser.Substring(browser.LastIndexOf("\\") + 1);
    }
    
    if (defBrowser != "iexplore")
    {
            Process.Start("IExplore.exe");
        ScreenScraperEngine.Instance.Wait(2000);
        string iepath = "";
        foreach (Process p in Process.GetProcesses())
        {
            if (p.ProcessName == "IEXPLORE")
            {
        iepath = p.MainModule.FileName;                         
            }
        }
        if (iepath != "")
            {
                string iepathval = "\"" + iepath + "\" -nohome";
                regkey.SetValue(null, iepathval);
            }
         }  
    
    0 讨论(0)
  • 2020-12-01 22:55

    The default browser is saved as an entry in the registry key of windows. The values are saved on a protocol basis like this

    HKEY_CLASSES_ROOT\[protocol]\shell\open\command

    Where protocol can be http, https, etc. On how to access/modify registry values inside C#, you can take a look at this article

    0 讨论(0)
  • 2020-12-01 23:13

    I think you will need to modify atleast two RegistryKeys and set the path to the alternative browser:

    HKEY_CLASSES_ROOT\http\shell\open\command
    HKEY_CLASSES_ROOT\htmlfile\shell\open\command
    

    An alternative may be to create an additional entry under the Shell key and set it as the default action:

    [HKEY_CLASSES_ROOT\http\shell]
    (default) set to OpenWithMyBrowser
    
    [HKEY_CLASSES_ROOT\http\shell\OpenWithMyBrowser\command]
    (default) set to "MyBrowser.exe"
    
    0 讨论(0)
提交回复
热议问题