Loading

C#获取默认浏览器的完整地址并在默认浏览器中打开对应的url地址

/*  
    使用注册表监视工具发现 \SOFTWARE\Clients\StartMenuInternet\ 记录了默认浏览器名字
    
      [HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\默认浏览器名字\shell\open\command] 记录了物理路径
*/
1
    /// <summary> 2 /// 获取默认浏览器的路径 3 /// </summary> 4 /// <returns></returns> 5 public String GetDefaultWebBrowserFilePath() 6 { 7 string _BrowserKey1 = @"Software\Clients\StartmenuInternet\"; 8 string _BrowserKey2 = @"\shell\open\command"; 9 10 RegistryKey _RegistryKey = Registry.CurrentUser.OpenSubKey(_BrowserKey1, false); 11 if (_RegistryKey == null) 12 _RegistryKey = Registry.LocalMachine.OpenSubKey(_BrowserKey1, false); 13 String _Result = _RegistryKey.GetValue("").ToString(); 14 _RegistryKey.Close(); 15 16 _RegistryKey = Registry.LocalMachine.OpenSubKey(_BrowserKey1 + _Result + _BrowserKey2); 17 _Result = _RegistryKey.GetValue("").ToString(); 18 _RegistryKey.Close(); 19 20 if (_Result.Contains("\"")) 21 { 22 _Result = _Result.TrimStart('"'); 23 _Result = _Result.Substring(0, _Result.IndexOf('"')); 24 } 25 return _Result; 26 } 27 28 /// <summary> 29 /// 获取当前默认浏览器的完整地址 30 /// </summary> 31 /// <param name="sender"></param> 32 /// <param name="e"></param> 33 private void btn_BrowserPath_Click(object sender, EventArgs e) 34 { 35 string BrowserPath = GetDefaultWebBrowserFilePath(); 36 txt_BrowserPath.Text = BrowserPath; 37 } 38 39 /// <summary> 40 /// 使用默认的浏览器打开指定的url地址 41 /// </summary> 42 /// <param name="sender"></param> 43 /// <param name="e"></param> 44 private void btn_gotoUrl_Click(object sender, EventArgs e) 45 { 46 string BrowserPath = GetDefaultWebBrowserFilePath(); 47 string gotoUrl = txt_url.Text; 48 if (!gotoUrl.StartsWith("http://")) 49 { 50 gotoUrl = "http://" + gotoUrl; 51 } 52 //如果输入的url地址为空,则清空url地址,浏览器默认跳转到默认页面 53 if (gotoUrl=="http://") 54 { 55 gotoUrl = ""; 56 } 57 System.Diagnostics.Process.Start(BrowserPath, gotoUrl); 58 }

 

posted @ 2013-01-14 21:51  jesn  阅读(3799)  评论(1编辑  收藏  举报