SoftwareTool.cs 4.0 KB

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