i am trying to write a program that close explorer then runs another program.
i am getting a problem when trying to close ex开发者_运维问答plorer 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
CHEERSp.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
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();
}
There is a simple undocumented way to exit explorer cleanly, see also question Gracefully Exit Explorer (Programmatically). It is intended for developers working on shell extensions.
The procedure is different for Windows XP and Windows 7:
Windows XP: Open the shutdown dialog (Start > Shutdown), then cancel the dialog pressing CTRL-SHIFT-ALT-ESC (or hold down CTRL-SHIFT-ALT and press the Button with the mouse).
Windows 7: Open the Start menu and then hold CTRL-SHIFT while right-klicking into the empty area of the start menu, see screenshot. A context menu appears, where the second entry is 'Exit Explorer' (without CTRL-SHIFT the context menu has only one entry)
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
Explorer is a critical Windows component. You should debug why you have problems when Explorer is running, and fix those.
Killing Explorer will cause severe problems for your users.
精彩评论