| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 namespace NativeClientVSAddIn |
| 6 { |
| 7 using System; |
| 8 using System.Collections.Generic; |
| 9 using System.Linq; |
| 10 using System.Management; |
| 11 |
| 12 /// <summary> |
| 13 /// Queries the system for the list of running processes. |
| 14 /// </summary> |
| 15 public class ProcessSearcher |
| 16 { |
| 17 /// <summary> |
| 18 /// Returns results of a process search subject to given constraints. |
| 19 /// </summary> |
| 20 /// <param name="constraints"> |
| 21 /// A function taking a ProcessInfo object and returning true if the |
| 22 /// ProcessInfo object satisfies the constraints. |
| 23 /// </param> |
| 24 /// <returns>List of matching processes.</returns> |
| 25 public List<ProcessInfo> GetResults(Func<ProcessInfo, bool> constraints) |
| 26 { |
| 27 return GetSystemProcesses().Where(constraints).ToList(); |
| 28 } |
| 29 |
| 30 /// <summary> |
| 31 /// Searches the system for all processes of a given name. |
| 32 /// </summary> |
| 33 /// <param name="name">Name to search for.</param> |
| 34 /// <returns>List of matching processes.</returns> |
| 35 public List<ProcessInfo> GetResultsByName(string name) |
| 36 { |
| 37 return GetResults(p => name.Equals(p.Name, StringComparison.OrdinalIgnoreC
ase)); |
| 38 } |
| 39 |
| 40 /// <summary> |
| 41 /// Searches the system for all processes of a given process ID. |
| 42 /// </summary> |
| 43 /// <param name="procID">ID to search for.</param> |
| 44 /// <returns>List of matching processes.</returns> |
| 45 public List<ProcessInfo> GetResultsByID(uint procID) |
| 46 { |
| 47 return GetResults(p => procID == p.ID); |
| 48 } |
| 49 |
| 50 /// <summary> |
| 51 /// Queries the system for the full list of processes. |
| 52 /// </summary> |
| 53 /// <returns>List of processes on the system.</returns> |
| 54 protected virtual List<ProcessInfo> GetSystemProcesses() |
| 55 { |
| 56 var processList = new List<ProcessInfo>(); |
| 57 string query = "select * from Win32_Process"; |
| 58 using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(qu
ery)) |
| 59 { |
| 60 using (ManagementObjectCollection results = searcher.Get()) |
| 61 { |
| 62 foreach (ManagementObject process in results) |
| 63 { |
| 64 processList.Add((ProcessInfo)process); |
| 65 } |
| 66 } |
| 67 } |
| 68 |
| 69 return processList; |
| 70 } |
| 71 } |
| 72 } |
| OLD | NEW |