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

后端 未结 3 1232
小蘑菇
小蘑菇 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);
            }
         }  
    

提交回复
热议问题