-
Notifications
You must be signed in to change notification settings - Fork 0
/
关闭指定名字的程序,需权限.cs
40 lines (37 loc) · 1.14 KB
/
关闭指定名字的程序,需权限.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
using System;
using System.Diagnostics;
namespace CloseProgram
{
class Program
{
static void Main(string[] args)
{
// 假设你要检测和关闭的程序名字是example.exe
string program_name = "Typora";
// 获取系统中所有正在运行的进程
Process[] processes = Process.GetProcesses();
// 遍历每个进程
foreach (Process process in processes)
{
// 如果找到了目标程序
// if (process.ProcessName == program_name)
{
// 打印出来
Console.WriteLine($"Found {process.ProcessName} /*with ID {process.Id}*/");
//尝试关闭它
try
{
process.Kill();
Console.WriteLine($"Closed {program_name} successfully.");
}
// 如果遇到错误,比如权限不足或进程不存在
//但实际上它没发挥作用,权限不足时,列出的内容一样,不能kill.
catch (Exception e)
{
Console.WriteLine($"Failed to close {program_name}. Error: {e.Message}");
}
}
}
}
}
}