JJohn
JJohn

Reputation: 175

How to change home page in Internet Explorer using C#?

Is it possible to change home page in Internet Explorer in C# application? Solution for other browsers (Firefox, Chrome) would be also nice.

Upvotes: 6

Views: 9003

Answers (5)

Vince I
Vince I

Reputation: 610

In addition to the above answers, you may want to check the following if there is a group policy in place: HKCU\Software\Policies\Microsoft\Internet Explorer\Main\Start Page

Upvotes: 0

Brandon J. Boone
Brandon J. Boone

Reputation: 16472

Yes you can. The home page is stored in the registry. As long as your C# program has rights to modify the registry, you should be able to change this entry to any page you want.

IE

[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main]
“Start Page”=”http://www.yourwebsite.com/”

How to modify the Window Registry

How to set the default browser home page (IE) with C#?

FireFox

Firefox does not store the home page in the registry because it has different profiles under $APPDATA\Mozilla\Firefox\Profiles[profile name] The file you need to read from is prefs.js, and the line: user_pref("browser.startup.homepage", .... );

To get the default profile you need to read $APPDATA\Mozilla\Firefox\profiles.ini. You'll have to loop through each [Profile#] until Default=1 and you'll have your profile name from Path=...

If you want me to put this into a function (sounds like a good idea too) or if you'd like to make it, then get it up on the Wiki.

-Stu

Source

Untested code from experts-exchange

public static void SetMozilla(string strURL)
        {
            try
            {
                string strSystemUname = Environment.UserName.ToString().Trim();
                string systemDrive = Environment.ExpandEnvironmentVariables("%SystemDrive%");
                string strDirectory = "";
                string strPrefFolder = "";
                if (Directory.Exists(systemDrive + "\\Documents and Settings\\" + strSystemUname + "\\Application Data\\Mozilla\\Firefox\\Profiles"))
                {
                    strDirectory = systemDrive + "\\Documents and Settings\\" + strSystemUname + "\\Application Data\\Mozilla\\Firefox\\Profiles";
                }
                else if (Directory.Exists(systemDrive + "\\WINDOWS\\Application Data\\Mozilla\\Firefox\\Profiles"))
                {
                    strDirectory = systemDrive + "\\WINDOWS\\Application Data\\Mozilla\\Firefox\\Profiles";
                }
                if (strDirectory.Trim().Length != 0)
                {
                    System.IO.DirectoryInfo oDir = new DirectoryInfo(strDirectory);
                    //System.IO.DirectoryInfo[] oSubDir;
                    //oSubDir = oDir.GetDirectories(strDirectory);
                    foreach (DirectoryInfo oFolder in oDir.GetDirectories())
                    {
                        if (oFolder.FullName.IndexOf(".default") >= 0)
                        {
                            strPrefFolder = oFolder.FullName;
                            CreatePrefs(strURL, strPrefFolder);
                        }
                    }

                }
            }
            catch
            { }
        }
        private static void CreatePrefs(string strURL, string strFolder)
        {
            StringBuilder sbPrefs = new StringBuilder();
            sbPrefs.Append("# Mozilla User Preferences\n\r");
            sbPrefs.Append("/* Do not edit this file.\n\r*\n\r"); 
            sbPrefs.Append("* If you make changes to this file while the application is running,\n\r");
            sbPrefs.Append("* the changes will be overwritten when the application exits.,\n\r*\n\r"); 
            sbPrefs.Append("* To make a manual change to preferences, you can visit the URL about:config\n\r");
            sbPrefs.Append("* For more information, see http://www.mozilla.org/unix/customizing.html#prefs\n\r");
            sbPrefs.Append("*/\n\r");
            sbPrefs.Append("user_pref(\"app.update.lastUpdateTime.addon-background-update-timer\", 1188927425);\n\r");
            sbPrefs.Append("user_pref(\"app.update.lastUpdateTime.background-update-timer\", 1188927425);\n\r");
            sbPrefs.Append("user_pref(\"app.update.lastUpdateTime.blocklist-background-update-timer\", 1188927425);\n\r");
            sbPrefs.Append("user_pref(\"app.update.lastUpdateTime.search-engine-update-timer\", 1188927425);\n\r");
            sbPrefs.Append("user_pref(\"browser.anchor_color\", \"#0000FF\");\n\r");
            sbPrefs.Append("user_pref(\"browser.display.background_color\", \"#C0C0C0\");\n\r");
            sbPrefs.Append("user_pref(\"browser.display.use_system_colors\", true);\n\r");
            sbPrefs.Append("user_pref(\"browser.formfill.enable\", false);\n\r");
            sbPrefs.Append("user_pref(\"browser.history_expire_days\", 20);\n\r");
            sbPrefs.Append("user_pref(\"browser.shell.checkDefaultBrowser\", false);\n\r");
            sbPrefs.Append("user_pref(\"browser.startup.homepage\", \"" + strURL +"\");\n\r");
            sbPrefs.Append("user_pref(\"browser.startup.homepage_override.mstone\", \"rv:1.8.1.6\");\n\r");
            sbPrefs.Append("user_pref(\"browser.visited_color\", \"#800080\");\n\r");
            sbPrefs.Append("user_pref(\"extensions.lastAppVersion\", \"2.0.0.6\");\n\r");
            sbPrefs.Append("user_pref(\"intl.charsetmenu.browser.cache\", \"UTF-8, ISO-8859-1\");\n\r");
            sbPrefs.Append("user_pref(\"network.cookie.prefsMigrated\", true);\n\r");
            sbPrefs.Append("user_pref(\"security.warn_entering_secure\", false);\n\r");
            sbPrefs.Append("user_pref(\"security.warn_leaving_secure\", false);\n\r");
            sbPrefs.Append("user_pref(\"security.warn_submit_insecure\", false);\n\r");
            sbPrefs.Append("user_pref(\"security.warn_submit_insecure.show_once\", false);\n\r");
            sbPrefs.Append("user_pref(\"spellchecker.dictionary\", \"en-US\");\n\r");
            sbPrefs.Append("user_pref(\"urlclassifier.tableversion.goog-black-enchash\", \"1.32944\");\n\r");
            sbPrefs.Append("user_pref(\"urlclassifier.tableversion.goog-black-url\", \"1.14053\");\n\r");
            sbPrefs.Append("user_pref(\"urlclassifier.tableversion.goog-white-domain\", \"1.23\");\n\r");
            sbPrefs.Append("user_pref(\"urlclassifier.tableversion.goog-white-url\", \"1.371\");\n\r");
            StreamWriter writer = new StreamWriter(strFolder + "\\prefs.js");
            writer.Write(sbPrefs.ToString()); 
            writer.Close();
            writer.Dispose();
            GC.Collect();
        }

Source

CHROME

Programmatically access the Google Chrome Home or Start page

Other Sources

Upvotes: 3

nunespascal
nunespascal

Reputation: 17724

Well, for IE, you have to set registry key:

HKCU\Software\Microsoft\Internet Explorer\Main\Start Page

For firefox, you will need to edit the js file: prefs.js. This can be found in: C:\Users\ [USERNAME]\AppData\Roaming\Mozilla\Firefox\Profiles\ [User Subfolder]

Chrome, Stores it data in: C:\Users\<username>\AppData\Local\Chromium\User Data\Default folder in a Preferences file. This is in JSON format. Editing it shouldn't be trouble

Upvotes: 12

Emond
Emond

Reputation: 50682

IE: edit the registery key HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Start Page

Firefox: find the settings in the profile folder (you will need to parse the file)

Chrome: find the default 'homepage' setting in the preferences file: C:\Users\%USERNAME%/AppData\AppData\Local\Google\Chrome\User Data\Default

For the registry use the .NET RegistryKey class. For the files you will need to parse the files and modify them.

Upvotes: 3

Harry Cutts
Harry Cutts

Reputation: 1424

The home page for Internet Explorer is held in the Start Page registry key at HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main (according to this), so you can set it using the Registry class in Microsoft.Win32 (from this example):

RegistryKey startPageKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main", true);
startPageKey.SetValue("Start Page", "http://stackoverflow.com");
startPageKey.Close();

Don't know about the others, I'm afraid.

Upvotes: 5

Related Questions