【发布时间】:2012-06-13 08:44:50
【问题描述】:
是否可以在 C# 应用程序中更改 Internet Explorer 中的主页?其他浏览器(Firefox、Chrome)的解决方案也不错。
【问题讨论】:
标签: c#
是否可以在 C# 应用程序中更改 Internet Explorer 中的主页?其他浏览器(Firefox、Chrome)的解决方案也不错。
【问题讨论】:
标签: c#
是的,你可以。主页存储在注册表中。只要您的 C# 程序有权修改注册表,您就应该能够将此条目更改为您想要的任何页面。
[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 不会将主页存储在注册表中,因为它有 $APPDATA\Mozilla\Firefox\Profiles[profile 下的不同配置文件 name] 您需要读取的文件是 prefs.js,行: user_pref("browser.startup.homepage", .... );
要获取您需要阅读的默认配置文件 $APPDATA\Mozilla\Firefox\profiles.ini。你必须循环通过 每个 [Profile#] 直到 Default=1,您的个人资料名称将从 路径=...
如果你想让我把它放到一个函数中(听起来是个好主意 也),或者如果您想制作它,请在 Wiki 上发布它。
-斯图
来自专家交流的未经测试的代码
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();
}
Programmatically access the Google Chrome Home or Start page
其他来源
【讨论】:
嗯,对于 IE,你必须设置注册表项:
HKCU\Software\Microsoft\Internet Explorer\Main\Start Page
对于 Firefox,您需要编辑 js 文件:prefs.js。
这可以在以下位置找到:C:\Users\ [USERNAME]\AppData\Roaming\Mozilla\Firefox\Profiles\ [User Subfolder]
Chrome,将其数据存储在:
Preferences 文件中的 C:\Users\<username>\AppData\Local\Chromium\User Data\Default 文件夹。
这是 JSON 格式。编辑它应该不麻烦
【讨论】:
Internet Explorer 的主页保存在HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main 的Start Page 注册表项中(根据this),因此您可以使用Microsoft.Win32 中的Registry 类进行设置(来自this example ):
RegistryKey startPageKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main", true);
startPageKey.SetValue("Start Page", "http://stackoverflow.com");
startPageKey.Close();
恐怕其他人就不知道了。
【讨论】:
IE:编辑注册表项 HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Start Page
Firefox:找到the settings in the profile folder(您需要解析文件)
Chrome:在首选项文件中找到默认的“主页”设置:C:\Users\%USERNAME%/AppData\AppData\Local\Google\Chrome\User Data\Default
对于注册表,请使用 .NET RegistryKey 类。对于文件,您需要解析文件并修改它们。
【讨论】:
除了上述答案之外,如果存在组策略,您可能还需要检查以下内容: HKCU\Software\Policies\Microsoft\Internet Explorer\Main\Start Page
【讨论】: