Exemple #1
0
        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}");
            }
        }
Exemple #2
0
 static void KillAndDelete(
     List <ProcessObj> procs,
     string searchText,
     bool recursive,
     CmdLine.CommandLine cmdLine
     )
 {
     foreach (var proc in procs)
     {
         try
         {
             DeleteEntry(searchText);
             Console.WriteLine($"Deleted \"{searchText}\"");
             return;
         }
         catch (Exception e) {
             Console.WriteLine($"Could not delete, {e.ToString()}");
         }
         Kill(proc, recursive, cmdLine);
     }
     DeleteEntry(searchText);
 }
Exemple #3
0
        static void Run(Options options)
        {
            var cmdline = new CmdLine.CommandLine(Print: false);

            string searchText = options.Path;

            if (!options.SearchMode)
            {
                searchText = Path.GetFullPath(searchText).Trim();
                if (searchText.EndsWith("\\"))
                {
                    searchText = searchText.Substring(0, searchText.Length - 1);
                }
            }

            // https://stackoverflow.com/questions/5510343/escape-command-line-arguments-in-c-sharp
            searchText = Regex.Replace(searchText, @"(\\+)$", @"$1$1");

            List <string> handles = cmdline.Run($"handle -nobanner \"{searchText}\"");

            handles = handles.Where(x => x.Trim().Length > 0).ToList();

            var pids = new Dictionary <int, HashSet <string> >();

            foreach (string handle in handles)
            {
                if (handle == "No matching handles found.")
                {
                    continue;
                }
                string pidStr = GetPid(handle);
                string path   = GetPath(handle);

                int pid = int.Parse(pidStr);
                if (!pids.ContainsKey(pid))
                {
                    pids.Add(pid, new HashSet <string>());
                    pids[pid].Add(path);
                }
            }

            List <ProcessObj> procs = new List <ProcessObj>();

            foreach (int pid in pids.Keys)
            {
                var paths     = pids[pid].ToArray();
                var processes = GetProcesses(pid, paths).ToList();
                foreach (var proc in processes)
                {
                    string name = proc.Name;
                    if (name == "explorer.exe")
                    {
                        // Don't kill the main explorer window, kill the actual windows using the files.
                        var explorerWindows = GetExplorerWindows(searchText, paths).ToList();
                        foreach (var explorerProc in explorerWindows)
                        {
                            explorerProc.WriteToConsole(options.NoPaths);
                            procs.Add(explorerProc);
                        }
                    }
                    else
                    {
                        proc.WriteToConsole(options.NoPaths);
                        procs.Add(proc);
                    }
                    break;
                }
            }

            if (options.NoKill)
            {
                return;
            }

            if (options.SearchMode)
            {
                if (procs.Count == 0)
                {
                    return;
                }
                if (!options.Kill)
                {
                    Console.WriteLine("\nDo you wish to kill the above processes(y/n)?");
                    char ch = Console.ReadKey().KeyChar;
                    if (ch != 'y')
                    {
                        return;
                    }
                }

                foreach (var proc in procs)
                {
                    Kill(proc, options.Recursive, cmdline);
                }
                return;
            }
            else if (!options.SearchMode)
            {
                KillAndDelete(procs, searchText, options.Recursive, cmdline);
            }
        }