3

If I kill explorer.exe like this:

private static void KillExplorer()
{
    var processes = Process.GetProcessesByName("explorer");
    Console.Write("Killing Explorer... ");
    foreach (var process in processes)
    {
        process.Kill();
        process.WaitForExit();
    }
    Console.WriteLine("Done");
}

It restarts immediately.

But if I use taskkill /F /IM explorer.exe, or kill it from the task manager, it doesn't restart.

Why is that? What's the difference? How can I close explorer.exe from code without restarting it? Sure, I could call taskkill from my code, but I was hoping for a cleaner solution...

8
  • 1
    What Windows version are you using?
    – t3hn00b
    Nov 27, 2012 at 11:25
  • 3
  • Are you trying to write a kiosk app?
    – Jodrell
    Nov 27, 2012 at 12:22
  • @t3hn00b, I tried it on XP and 7, with the same results. Nov 27, 2012 at 13:47
  • @DanBarzilay, the accepted answer to this question doesn't work, and the others are not very helpful either. Please don't close. Nov 27, 2012 at 13:49

2 Answers 2

5

I can't say that I haven't cheated to get the answer. All credits go to morguth for his post here.

What he has suggested (and proved work on my Win7 and XPMode) is that there is a registry key that forces the shell to restart automatically. By using the following code you disable that.

RegistryKey ourKey = Registry.LocalMachine;
ourKey = ourKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);
ourKey.SetValue("AutoRestartShell", 0);
// Kill the explorer by the way you've post and do your other work
ourKey.SetValue("AutoRestartShell", 1)
3
  • Thanks for you answer. I've seen this post before, but it doesn't seem like a very good idea to change a system-wide setting just for a one-shot action. What if a crash occurs after you set the value to 0 and before you set it back to 1? Nov 27, 2012 at 17:40
  • @ThomasLevesque well my home Win has that option disabled (set to 0) that's why I've asked you about the Windows version (I thought it was version specific). So I think nothing problematic will happen yet you can test it yourself - googling around didn't show any strange behaviour. NVidia driver installer kills the explorer when installing new drivers, so if they do it (and they probably do it roughly the same way) I doubt anything really dangerous might happen. Yet it's up to you and your requirements.
    – t3hn00b
    Nov 28, 2012 at 12:03
  • Old, very old, but wouldn't it be better to save the current value of the registry key and restore it to that value when you're done ?
    – bart s
    Oct 16, 2023 at 12:25
0

instead of using Process.Kill, try this:

Process.Start(@"C:\Windows\System32\taskkill.exe", @"/F /IM explorer.exe"); 
2
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 27, 2022 at 7:50
  • The OP said: "Sure, I could call taskkill from my code, but I was hoping for a cleaner solution"
    – LesFerch
    Jan 14 at 3:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .