SoftwareTool.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using Y.Utils.DataUtils.Collections;
  9. namespace Y.Utils.WindowsUtils.InfoUtils
  10. {
  11. public class SoftwareTool
  12. {
  13. /// <summary>
  14. /// 存在控制面板
  15. /// </summary>
  16. /// <param name="name"></param>
  17. /// <returns></returns>
  18. public static bool ExistControl(string name)
  19. {
  20. if (GetControlList().Contains(name))
  21. return true;
  22. return false;
  23. }
  24. public static bool ExistControl(string[] names)
  25. {
  26. bool flag = false;
  27. if (!ListTool.IsNullOrEmpty(names))
  28. {
  29. foreach (var n in names)
  30. {
  31. if (ExistControl(n))
  32. return true;
  33. }
  34. }
  35. return flag;
  36. }
  37. public static List<string> GetControlList()
  38. {
  39. List<string> result = new List<string>();
  40. result.AddRange(GetControlList(Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")));
  41. result.AddRange(GetControlList(Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall")));
  42. result.AddRange(GetControlList(Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")));
  43. result.AddRange(GetControlList(Registry.CurrentUser.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall")));
  44. return result;
  45. }
  46. private static List<string> GetControlList(RegistryKey key)
  47. {
  48. List<string> result = new List<string>();
  49. try
  50. {
  51. if (key != null)//如果系统禁止访问则返回null
  52. {
  53. foreach (string SubKeyName in key.GetSubKeyNames())
  54. {
  55. //打开对应的软件名称
  56. RegistryKey SubKey = key.OpenSubKey(SubKeyName);
  57. if (SubKey != null)
  58. {
  59. String SoftwareName = SubKey.GetValue("DisplayName", "Nothing").ToString();
  60. //如果没有取到,则不存入动态数组
  61. if (SoftwareName != "Nothing")
  62. {
  63. result.Add(SoftwareName.Trim());
  64. }
  65. }
  66. SubKey?.Close();
  67. }
  68. }
  69. key?.Close();
  70. }
  71. catch { }
  72. return result;
  73. }
  74. /// <summary>
  75. /// 存在进程
  76. /// </summary>
  77. /// <param name="name"></param>
  78. /// <returns></returns>
  79. public static bool ExistProcess(string name)
  80. {
  81. try
  82. {
  83. var p = Process.GetProcessesByName(name);
  84. if (p != null && p.Length == 0)
  85. return false;
  86. else
  87. return true;
  88. }
  89. catch { }
  90. return false;
  91. }
  92. public static bool ExistProcess(string[] names)
  93. {
  94. bool flag = false;
  95. if (!ListTool.IsNullOrEmpty(names))
  96. {
  97. foreach (var n in names)
  98. {
  99. if (ExistProcess(n))
  100. return true;
  101. }
  102. }
  103. return flag;
  104. }
  105. /// <summary>
  106. /// 存在文件
  107. /// </summary>
  108. /// <param name="name"></param>
  109. /// <returns></returns>
  110. public static bool ExistFile(string name)
  111. {
  112. if (File.Exists(name))
  113. return true;
  114. return false;
  115. }
  116. public static bool ExistFile(string[] names)
  117. {
  118. bool flag = false;
  119. if (!ListTool.IsNullOrEmpty(names))
  120. {
  121. foreach (var n in names)
  122. {
  123. if (ExistFile(n))
  124. return true;
  125. }
  126. }
  127. return flag;
  128. }
  129. /// <summary>
  130. /// 存在注册表项
  131. /// </summary>
  132. /// <param name="item"></param>
  133. /// <param name="key"></param>
  134. /// <returns></returns>
  135. public static bool ExistRegist(string item, string key)
  136. {
  137. try
  138. {
  139. object obj = Registry.GetValue(item, key, null);
  140. if (obj != null)
  141. return true;
  142. }
  143. catch { }
  144. return false;
  145. }
  146. }
  147. }