N
N
Nikolai2021-10-14 12:47:29
C++ / C#
Nikolai, 2021-10-14 12:47:29

How to close an explorer instance via its pid?

The task says to open explorers with the K key and close all open explorers with the x key. I managed to close them by the name of the program (explorer.exe), but at the same time the system rebooted. By pid it is not possible to close open conductors at all

List ids = new List();
for (int i =0; i<5; i++)
{
Word = Console.ReadLine();
Word_to_2 = StringToBinary(Word);

switch (Word_to_2)
{
case "01001011":
Process p = Process.Start("explorer.exe");
ids.Add(p.Id);
break;
case "01010110":
Console.WriteLine("Введите путь");
Path = Console.ReadLine();
break;
case "01000010":
Console.WriteLine("Введите название файла");
File_name = Console.ReadLine();
FileStream fileStream = File.Create(Path + File_name);
StreamWriter output = new StreamWriter(fileStream);
output.Close();
break;
case "01101110":
File.Delete(Path + File_name);
break;
case "01111000":

foreach (var e in ids)
{
Process pp = Process.GetProcessById(e);
pp.Kill();
}
break;
}
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2021-10-14
@Adler_lug

Process.GetProcessById()

M
Maxim Grishin, 2021-10-14
@vesper-bot

You open explorers as child processes, remember their PIDs, then kill them when necessary. The function, how to get the process by PID, was written above.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question