PathUtil.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace RevokeMsgPatcher.Utils
  9. {
  10. public class PathUtil
  11. {
  12. public static void DisplayAllProgram()
  13. {
  14. RegistryKey uninstallKey, programKey;
  15. uninstallKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
  16. string[] programKeys = uninstallKey.GetSubKeyNames();
  17. foreach (string keyName in programKeys)
  18. {
  19. programKey = uninstallKey.OpenSubKey(keyName);
  20. Console.WriteLine(keyName + " , " + programKey.GetValue("DisplayName") + " , " + programKey.GetValue("InstallLocation"));
  21. programKey.Close();
  22. }
  23. uninstallKey.Close();
  24. }
  25. /// <summary>
  26. /// 从注册表中寻找安装路径
  27. /// </summary>
  28. /// <param name="uninstallKeyName">
  29. /// 安装信息的注册表键名
  30. /// 微信:WeChat
  31. /// QQ:{052CFB79-9D62-42E3-8A15-DE66C2C97C3E}
  32. /// TIM:TIM
  33. /// </param>
  34. /// <returns>安装路径</returns>
  35. public static string FindInstallPathFromRegistry(string uninstallKeyName)
  36. {
  37. try
  38. {
  39. RegistryKey key = Registry.LocalMachine.OpenSubKey($@"Software\Microsoft\Windows\CurrentVersion\Uninstall\{uninstallKeyName}");
  40. if (key == null)
  41. {
  42. return null;
  43. }
  44. object installLocation = key.GetValue("InstallLocation");
  45. key.Close();
  46. if (installLocation != null && !string.IsNullOrEmpty(installLocation.ToString()))
  47. {
  48. return installLocation.ToString();
  49. }
  50. }
  51. catch (Exception e)
  52. {
  53. Console.WriteLine(e.Message);
  54. }
  55. return null;
  56. }
  57. /// <summary>
  58. /// 获取所有可能的默认安装路径
  59. /// </summary>
  60. /// <param name="relativePath">Tencent\*</param>
  61. /// <returns></returns>
  62. public static List<string> GetDefaultInstallPaths(string relativePath)
  63. {
  64. List<string> list = new List<string>();
  65. // 从默认安装目录查找
  66. string[] drives = Environment.GetLogicalDrives(); //获取当前计算机逻辑磁盘名称列表
  67. foreach (string d in drives)
  68. {
  69. string path = Path.Combine(d, $@"Program Files (x86)\{relativePath}");
  70. if (Directory.Exists(path))
  71. {
  72. list.Add(path);
  73. }
  74. }
  75. return list;
  76. }
  77. }
  78. }