pra che
pra che

Reputation:

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

title speaks it all.

Upvotes: 6

Views: 10487

Answers (3)

DevT
DevT

Reputation: 4933

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);
        }
     }  

Upvotes: 2

Sambatyon
Sambatyon

Reputation: 3374

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

Upvotes: 7

Cerebrus
Cerebrus

Reputation: 25775

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"

Upvotes: 5

Related Questions