| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using Y.Utils.DataUtils.Collections;
- namespace Y.Utils.WindowsUtils.InfoUtils
- {
- public class SoftwareTool
- {
- /// <summary>
- /// 存在控制面板
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static bool ExistControl(string name)
- {
- if (GetControlList().Contains(name))
- return true;
- return false;
- }
- public static bool ExistControl(string[] names)
- {
- bool flag = false;
- if (!ListTool.IsNullOrEmpty(names))
- {
- foreach (var n in names)
- {
- if (ExistControl(n))
- return true;
- }
- }
- return flag;
- }
- public static List<string> GetControlList()
- {
- List<string> result = new List<string>();
- result.AddRange(GetControlList(Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")));
- result.AddRange(GetControlList(Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall")));
- result.AddRange(GetControlList(Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")));
- result.AddRange(GetControlList(Registry.CurrentUser.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall")));
- return result;
- }
- private static List<string> GetControlList(RegistryKey key)
- {
- List<string> result = new List<string>();
- try
- {
- if (key != null)//如果系统禁止访问则返回null
- {
- foreach (string SubKeyName in key.GetSubKeyNames())
- {
- //打开对应的软件名称
- RegistryKey SubKey = key.OpenSubKey(SubKeyName);
- if (SubKey != null)
- {
- String SoftwareName = SubKey.GetValue("DisplayName", "Nothing").ToString();
- //如果没有取到,则不存入动态数组
- if (SoftwareName != "Nothing")
- {
- result.Add(SoftwareName.Trim());
- }
- }
- SubKey?.Close();
- }
- }
- key?.Close();
- }
- catch { }
- return result;
- }
- /// <summary>
- /// 存在进程
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static bool ExistProcess(string name)
- {
- try
- {
- var p = Process.GetProcessesByName(name);
- if (p != null && p.Length == 0)
- return false;
- else
- return true;
- }
- catch { }
- return false;
- }
- public static bool ExistProcess(string[] names)
- {
- bool flag = false;
- if (!ListTool.IsNullOrEmpty(names))
- {
- foreach (var n in names)
- {
- if (ExistProcess(n))
- return true;
- }
- }
- return flag;
- }
- /// <summary>
- /// 存在文件
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static bool ExistFile(string name)
- {
- if (File.Exists(name))
- return true;
- return false;
- }
- public static bool ExistFile(string[] names)
- {
- bool flag = false;
- if (!ListTool.IsNullOrEmpty(names))
- {
- foreach (var n in names)
- {
- if (ExistFile(n))
- return true;
- }
- }
- return flag;
- }
- /// <summary>
- /// 存在注册表项
- /// </summary>
- /// <param name="item"></param>
- /// <param name="key"></param>
- /// <returns></returns>
- public static bool ExistRegist(string item, string key)
- {
- try
- {
- object obj = Registry.GetValue(item, key, null);
- if (obj != null)
- return true;
- }
- catch { }
- return false;
- }
- }
- }
|