如何使用c#或批处理文件更改默认浏览器

时间:2009-06-09 06:23:18

标签: browser default

标题说明了一切。

3 个答案:

答案 0 :(得分:7)

默认浏览器将保存为Windows注册表项中的条目。这些值保存在协议基础上,如

  

HKEY_CLASSES_ROOT \ [协议] \壳\开放\命令

协议可以是http,https等。关于如何访问/修改C#中的注册表值,您可以查看this article

答案 1 :(得分:5)

我认为您需要修改至少两个RegistryKeys并设置替代浏览器的路径:

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

alternative可能是在Shell密钥下创建一个附加条目并将其设置为默认操作:

[HKEY_CLASSES_ROOT\http\shell]
(default) set to OpenWithMyBrowser

[HKEY_CLASSES_ROOT\http\shell\OpenWithMyBrowser\command]
(default) set to "MyBrowser.exe"

答案 2 :(得分:2)

对于Windows 7,您需要更改

的注册表项
HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\ Associations\UrlAssociations\http

您可以使用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");
 }

用于vista os之前 - (在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);
        }
     }  
相关问题