判断用户使用的默认浏览器

作者:追风剑情 发布于:2019-3-24 23:25 分类:C#

示例

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApp2
  7. {
  8. class Program
  9. {
  10. public enum BrowserApplication
  11. {
  12. Unknown,
  13. InternetExplorer,
  14. Firefox,
  15. Chrome,
  16. Opera,
  17. Safari,
  18. Edge,
  19. }
  20.  
  21. static void Main(string[] args)
  22. {
  23. BrowserApplication currentBrowser = GetDefaultBrowser();
  24.  
  25. Console.WriteLine("当前使用的默认浏览器为: {0}", currentBrowser);
  26.  
  27. Console.Read();
  28. }
  29.  
  30. public static BrowserApplication GetDefaultBrowser()
  31. {
  32. //通过检查注册表(regedit)项来判断用户使用的默认浏览器
  33. //HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice\ProgId
  34. const string userChoice = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice";
  35. using (Microsoft.Win32.RegistryKey userChoiceKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(userChoice))
  36. {
  37. if (userChoiceKey == null)
  38. {
  39. return BrowserApplication.Unknown;
  40. }
  41.  
  42. object progIdValue = userChoiceKey.GetValue("Progid");
  43. if (progIdValue == null)
  44. {
  45. return BrowserApplication.Unknown;
  46. }
  47.  
  48. string browserId = progIdValue.ToString().ToLower();
  49.  
  50. if (browserId.Contains("ie.http"))
  51. return BrowserApplication.InternetExplorer;
  52. else if (browserId.Contains("firefox"))
  53. return BrowserApplication.Firefox;
  54. else if (browserId.Contains("chrome"))
  55. return BrowserApplication.Chrome;
  56. else if (browserId.Contains("opera"))
  57. return BrowserApplication.Opera;
  58. else if (browserId.Contains("safari"))
  59. return BrowserApplication.Safari;
  60. else if (browserId.Contains("appcq0fevzme2pys62n3e0fbqa7peapykr8v")) //AppXq0fevzme2pys62n3e0fbqa7peapykr8v
  61. return BrowserApplication.Edge;
  62. else
  63. return BrowserApplication.Unknown;
  64. }
  65. }
  66. }
  67. }

运行测试

1111.png

2222.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号