5

Strange, but perhaps I am handling it the incorrect way - I need to quite simply check if explorer.exe is running, and if so kill it. However, the way I am currently achieving this, explorer.exe simply restarts after I kill it.

Normal taskkill through batch works fine though, does C# do something different?

private void Form1_Load(object sender, EventArgs e)
{
    Process[] prcChecker = Process.GetProcessesByName("explorer");
    if (prcChecker.Length > 0)
    {
        MessageBox.Show("Explorer running");
        foreach (Process p in prcChecker)
        {
            p.Kill();
        }
    }
    else
    {
        MessageBox.Show("Explorer is not running");
    }
}
Marc
  • 3,905
  • 4
  • 21
  • 37
PnP
  • 3,133
  • 17
  • 62
  • 95
  • 1
    This is a duplicate question! Solution: http://superuser.com/questions/511914/why-does-explorer-restart-automatically-when-i-kill-it-with-process-kill – olydis Sep 14 '13 at 18:02
  • How is that superuser material? It's C#.... I get a downvote for not looking on the wrong site :( – PnP Sep 14 '13 at 18:03
  • indeed ;) but hey, they answered it :) – olydis Sep 14 '13 at 18:04
  • considering that googling for 1s reveals the answer to the EXACT same question... yes – olydis Sep 14 '13 at 18:05
  • 1
    Maybe, but look here: @DanBarzilay, the accepted answer to this question doesn't work, and the others are not very helpful either. Please don't close. – Thomas Levesque Nov 27 '12 at 13:49 Would also require elevated privileges to execute. – PnP Sep 14 '13 at 18:06
  • oh weird... because it does work for me, did this few months ago :) that is actually why I already knew the post on superuser ;) – olydis Sep 14 '13 at 18:08
  • please take a look at this link on how to this efficiently http://stackoverflow.com/questions/2570244/problem-with-killing-windows-explorer – BRAHIM Kamel Sep 14 '13 at 18:11
  • So if it's working with Batch properly, why don't you execute the batch file via C#?` – Daniel Abou Chleih Sep 14 '13 at 18:16

3 Answers3

2

That's because Windows takes care of restarting explorer.exe if it happens to die.

It is possible to delay this behavior (the setup of tortoisegit does this, for example), but it's not recommended - users are going to be pissed.

zmbq
  • 38,013
  • 14
  • 101
  • 171
2

Although not C# way but you can alternatively try to set the registry key HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoRestartShell to 0 to stop the auto restart.

EDIT:-

Try this in C#:-

RegistryKey ourKey = Registry.LocalMachine;
ourKey = ourKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);
ourKey.SetValue("AutoRestartShell", 0);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

Try to kill the Process with exit code 1 .

Sorry i dont have any example code because i am not a C# programmer but in my application it worked just fine.

I used the C++ Function:

TerminateProcess

Tomke
  • 35
  • 7