简体   繁体   中英

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");
    }
}

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.

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM