项目中用到VPN技术登录来访问内部网络的应用系统,VPN客户端连接后会自动以默认浏览器来打开站点,由于应用系统使用的前端框架对IE浏览器版本要求较高,而用户大多数的电脑里安装的IE的版本都较低,于是想到U盘里放上免安装的浏览器,接着通过执行一个程序自动将U盘里的浏览器设置为默认浏览器。winXP和win7设置默认浏览器的注册表项会有所不同,具体C#代码如下:
public static void CreateUSBWebUrlForWin7() { string currentPath = System.AppDomain.CurrentDomain.BaseDirectory; RegistryKey regCR = Registry.ClassesRoot; RegistryKey regCommand = null; if (!IsExistSubKey(regCR, DIY_KEY_NAME)) { //创建自定义的注册表项 regCommand = regCR.CreateSubKey(DIY_KEY_NAME, RegistryKeyPermissionCheck.ReadWriteSubTree) .CreateSubKey("Shell", RegistryKeyPermissionCheck.ReadWriteSubTree) .CreateSubKey("open", RegistryKeyPermissionCheck.ReadWriteSubTree) .CreateSubKey("command", RegistryKeyPermissionCheck.ReadWriteSubTree); } else { //打开已存在的自定义注册表项 regCommand = regCR.OpenSubKey(DIY_KEY_NAME, true) .OpenSubKey("Shell", true) .OpenSubKey("open", true) .OpenSubKey("command", true); } regCommand.SetValue("", "\"" + currentPath + "\\360se6\\Application\\360se.exe\" -- \"%1\""); RegistryKey regCU = Registry.CurrentUser; RegistryKey regUrlAssoc = regCU.OpenSubKey("Software", true).OpenSubKey("Microsoft", true).OpenSubKey("Windows", true) .OpenSubKey("Shell", true) .OpenSubKey("Associations", true) .OpenSubKey("UrlAssociations", true); RegistryKey regHttpChoice = regUrlAssoc.OpenSubKey("http", true).OpenSubKey("UserChoice", true); regHttpChoice.SetValue("Progid", DIY_KEY_NAME); RegistryKey regHttpsChoice = regUrlAssoc.OpenSubKey("https", true).OpenSubKey("UserChoice", true); regHttpsChoice.SetValue("Progid", DIY_KEY_NAME); }
public static void CreateUSBWebUrlForWinXP() { string currentPath = System.AppDomain.CurrentDomain.BaseDirectory; RegistryKey regCR = Registry.ClassesRoot; RegistryKey regCRhttp = regCR.OpenSubKey("http", true).OpenSubKey("shell", true).OpenSubKey("open", true); RegistryKey regHttpCmd = regCRhttp.OpenSubKey("command", true); regHttpCmd.SetValue("", "\"" + currentPath + "\\360se6\\Application\\360se.exe\" -- \"%1\""); RegistryKey regCRhttps = regCR.OpenSubKey("https", true).OpenSubKey("shell", true).OpenSubKey("open", true); RegistryKey regHttpsCmd = regCRhttps.OpenSubKey("command", true); regHttpsCmd.SetValue("", "\"" + currentPath + "\\360se6\\Application\\360se.exe\" -- \"%1\""); }
private static bool IsExistSubKey(RegistryKey key,string subKeyName) { string[] subKeyNames = key.GetSubKeyNames(); foreach (string keyName in subKeyNames) { if (keyName == subKeyName) { return true; } } return false; }
对于系统是winXP还是win7的判断可以用以下代码判断:
int sysVer = System.Environment.OSVersion.Version.Major; switch (sysVer) { case 5: CreateUSBWebUrlForWinXP(); break; case 6: CreateUSBWebUrlForWin7(); break; }