forked from XenHat/TrayReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessDestroyer.cs
91 lines (87 loc) · 3.24 KB
/
ProcessDestroyer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
namespace TrayApp
{
public class ProcessDestroyer
{
public static void KillProcessByName(List<string> processToKill)
{
if (processToKill == null)
return;
List<string> wanted_dead = TrayApp.Properties.Settings.Default.KnownGPUProcesses;
Console.WriteLine("Will attempt to kill:");
foreach (var w in wanted_dead)
{
Console.WriteLine(w);
}
List<Process> listResult = new List<Process>();
List<Process> processes_list = Process.GetProcesses().ToList();
for (int i = 0; i < processes_list.Count; i++)
{// Iterate through processes running on machine
var rProcess = processes_list[i];
var ID = rProcess.Id;
var rName = rProcess.ProcessName;
var rWTitle = rProcess.MainWindowTitle;
for (int n = 0; n < wanted_dead.Count; n++)
{// Compare against processes we want to kill
var wtitle = wanted_dead[n];
if (wtitle == rWTitle)
{
//Console.WriteLine("Yes! (title title match)");
}
else if (wtitle == rName)
{
//Console.WriteLine("Yes! (process name match)");
}
else
{
continue;
}
Console.WriteLine("Killing " + rName);
try
{
rProcess.Kill();
// Note: This will throw an exception if the process has already died.
rProcess.WaitForExit();
rProcess.Dispose();
}
catch
{
// Don't crash; do nothing.
}
}
}
//Console.WriteLine("Done.");
}
}
}
public static class ProcessEx
{
// This is used to find the command line (aka invoking params and all) a process was launched with. Currently unused, but will be useful later.
public static string GetCommandLine(this Process process)
{
System.Text.StringBuilder commandLine = new System.Text.StringBuilder();
try
{
commandLine = new System.Text.StringBuilder(process.MainModule.FileName);
commandLine.Append(" ");
using (var searcher = new System.Management.ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id))
{
foreach (var @object in searcher.Get())
{
commandLine.Append(@object["CommandLine"]);
commandLine.Append(" ");
}
}
}
catch
{
}
return commandLine.ToString();
}
}