Why does explorer restart automatically when I kill it with Process.Kill?

8,005

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)
Share:
8,005

Related videos on Youtube

Thomas Levesque
Author by

Thomas Levesque

I'm a French C# developer from Paris. I work mostly on ASP.NET Core projects. I'm a Microsoft MVP (Development technologies) since 2012.

Updated on September 18, 2022

Comments

  • Thomas Levesque
    Thomas Levesque over 1 year

    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...

    • t3hn00b
      t3hn00b over 11 years
      What Windows version are you using?
    • Dan Barzilay
      Dan Barzilay over 11 years
    • Jodrell
      Jodrell over 11 years
      Are you trying to write a kiosk app?
    • Thomas Levesque
      Thomas Levesque over 11 years
      @t3hn00b, I tried it on XP and 7, with the same results.
    • Thomas Levesque
      Thomas Levesque over 11 years
      @DanBarzilay, the accepted answer to this question doesn't work, and the others are not very helpful either. Please don't close.
    • Thomas Levesque
      Thomas Levesque over 11 years
      @Jodrell, no, I'm trying to write a setup for an application; the application installs a shell extension, so I need to kill explorer before I can overwrite a previous installation.
    • Thomas Levesque
      Thomas Levesque over 11 years
      Why was this post migrated? It has nothing to do on SuperUser...
  • Thomas Levesque
    Thomas Levesque over 11 years
    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?
  • t3hn00b
    t3hn00b over 11 years
    @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.
  • Community
    Community about 2 years
    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.