Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically close windows explorer

i am trying to write a program that close explorer then runs another program.
i am getting a problem when trying to close explorer using the following code:

foreach (Process p in Process.GetProcesses())
                if (p.MainModule.ModuleName.Contains("explorer"))
                    p.Kill();  

can somebody please let me know why it is doing this and provide a solution
CHEERS

p.s. this is not a malicous program, it is going to run a game that doesn't work properly when explorer is in the background

like image 299
harryovers Avatar asked Dec 29 '22 00:12

harryovers


1 Answers

The problem is that you can have multiple versions of Explorer running at any one point in time... and you usually need at least one of them. The shell that hosts the Start Menu is actually an instance of Explorer. So if you close all instances of Explorer, you'll also be shutting down the main shell, which is not what you want to do.

However, the fastest way to do get all instances of Explorer and kill them is:

foreach (Process p in Process.GetProcessesByName("explorer"))
{
   p.Kill();
}
like image 84
Nick Avatar answered Jan 12 '23 02:01

Nick