SoftwareTool.cs 4.7 KB

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