Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill Explorer process

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");
    }
}
like image 979
PnP Avatar asked Oct 03 '22 23:10

PnP


2 Answers

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.

like image 148
zmbq Avatar answered Oct 07 '22 18:10

zmbq


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);
like image 28
Rahul Tripathi Avatar answered Oct 07 '22 19:10

Rahul Tripathi