Last active
March 15, 2024 02:09
-
-
Save ImShizer/cdb46f3c657ce3d00ac0a331756f54c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Threading; | |
class Program | |
{ | |
static bool isRunning = true; | |
static void Main() | |
{ | |
while (true) | |
{ | |
isRunning = true; | |
int res; | |
Console.Write("Выберите действие\n" + | |
"[1] Просмотреть обновляемый список процессов\n" + | |
"[2] Завершить процесс\n" + | |
"[3] Запустить процесс по имени\n" + | |
"[4] Просмотреть подробно о процессе\n" + | |
"[5] Выход\n" + | |
">> "); | |
res = Convert.ToInt32(Console.ReadLine()); | |
switch (res) | |
{ | |
case 1: | |
Thread thread = new Thread(ShowProcessInformation); | |
thread.Start(); | |
Console.WriteLine("Нажмите любую клавишу, чтобы остановить..."); | |
Console.ReadKey(); | |
isRunning = false; | |
if (thread.IsAlive) | |
thread.Join(); | |
break; | |
case 2: | |
int id; | |
Console.Write("Введите ID процесса: "); | |
id = Convert.ToInt32(Console.ReadLine()); | |
KillProcess(id); | |
break; | |
case 3: | |
string name; | |
Console.Write("Введите имя процесса: "); | |
name = Console.ReadLine(); | |
ProcessStartInfo startInfo = new ProcessStartInfo(); | |
startInfo.FileName = name; | |
try | |
{ | |
using (Process process = Process.Start(startInfo)) | |
{ | |
Console.WriteLine("Процесс запущен"); | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine($"Возникла ошибка: {e.Message}"); | |
} | |
break; | |
case 4: | |
Console.Write("Введите ID процесса: "); | |
id = Convert.ToInt32(Console.ReadLine()); | |
DetailedInfo(id); | |
break; | |
default: | |
return; | |
} | |
} | |
} | |
static void ShowProcessInformation() | |
{ | |
while (isRunning) | |
{ | |
Console.Clear(); | |
Process[] processes = Process.GetProcesses(); | |
foreach (Process process in processes) | |
{ | |
Console.WriteLine($"ID: {process.Id}, Имя: {process.ProcessName}"); | |
} | |
Console.WriteLine("Нажмите любую клавишу, чтобы остановить просмотр...\n"); | |
Thread.Sleep(3000); | |
} | |
} | |
static void DetailedInfo(int processId) | |
{ | |
try | |
{ | |
Process process = Process.GetProcessById(processId); | |
Console.WriteLine("=================================="); | |
Console.WriteLine($"Процесс с ID {processId} найден:"); | |
Console.WriteLine($"Имя: {process.ProcessName}"); | |
Console.WriteLine($"Подписан как: {process.MainModule.FileVersionInfo.FileDescription}"); | |
Console.WriteLine($"Путь: {process.MainModule.FileName}"); | |
Console.WriteLine("=================================="); | |
} | |
catch (ArgumentException) | |
{ | |
Console.WriteLine($"Процесс с ID {processId} не найден."); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Возникла ошибка при нахождении процесса: {ex.Message}"); | |
} | |
} | |
static void KillProcess(int processId) | |
{ | |
try | |
{ | |
Process process = Process.GetProcessById(processId); | |
process.Kill(); | |
Console.WriteLine($"Процесс с ID {processId} был завершён."); | |
} | |
catch (ArgumentException) | |
{ | |
Console.WriteLine($"Процесс с ID {processId} не найден."); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Возникла ошибка при завершении процесса: {ex.Message}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment