static void Kill(ProcessObj procObj, bool recursive, CmdLine.CommandLine cmdLine) { if (recursive) { try { cmdLine.Run($"taskkill /F /PID {procObj.ProcessId}"); } catch (Exception e) { Console.WriteLine($"{procObj.Name}({procObj.ProcessId}) could not be killed, {e.Message}"); } } if (procObj.Process == null) { Console.WriteLine($"{procObj.Name}({procObj.ProcessId}) already killed"); return; } try { procObj.Process.Kill(); Console.WriteLine($"{procObj.Name}({procObj.ProcessId}) killed"); } catch (Exception e) { Console.WriteLine($"{procObj.Name}({procObj.ProcessId}) could not be killed, {e.Message}"); } }
public static IEnumerable <ProcessObj> GetExplorerWindows(string searchQuery, string[] paths) { string wmiQuery = $"SELECT Name, ProcessID, CommandLine, Handle, ThreadCount, WorkingSetSize, Caption, ParentProcessId FROM Win32_Process WHERE Name like \"%explorer%\""; ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery); ManagementObjectCollection retObjectCollection = searcher.Get(); foreach (ManagementObject retObject in retObjectCollection) { string[] argList = new string[] { "", "" }; //uint ownerId = (uint)retObject.InvokeMethod("GetOwner", argList); var obj = new ProcessObj(retObject, argList[0], argList[1], paths); // Only for for a substring of the searchQuery... because titles are only so many characters long (around 87 characters), // so long strings can't be matched. if (searchQuery.Length > 86) { searchQuery = searchQuery.Substring(0, 86); } if (obj.Process.MainWindowTitle.Contains(searchQuery)) { foreach (var subProc in GetProcesses(obj.ProcessId, paths)) { yield return(subProc); } } } }