views:

146

answers:

2

Hi,
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

A: 

Why do you look for explorer in the ModuleName and not the process name? It should work with the regular process name.

eWolf
+2  A: 

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();
}
Nick
this is what i want to do, i do it manually at the moment in order to play the game. i thought it would be nicer if i had one button to close explorer then start the game and another to start explorer
harryovers
@harryovers - OK... I made an edit to show the proper code then.
Nick
@harryovers - Also as was mentioned in a different comment... the Shell generally restarts automatically if it's shut down... so this may not work very well.
Nick
it is restarting every time, nevermind. will just have to do it manually
harryovers