当我用 Process.Kill 杀死它时,为什么资源管理器会自动重新启动?

Tho*_*que 3 c# windows-explorer kill process .net-framework

如果我像这样杀死 explorer.exe:

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");
}
Run Code Online (Sandbox Code Playgroud)

它立即重新启动。

但是如果我使用taskkill /F /IM explorer.exe,或者从任务管理器中杀死它,它就不会重新启动。

这是为什么?有什么不同?如何在不重新启动的情况下从代码中关闭 explorer.exe?当然,我可以从我的代码中调用 taskkill,但我希望有一个更清晰的解决方案......

小智 5

我不能说我没有作弊得到答案。所有的功劳都归于 morguth在这里的职位。

他建议(并在我的 Win7 和 XPMode 上证明有效)是有一个注册表项可以强制 shell 自动重新启动。通过使用以下代码,您可以禁用它。

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)
Run Code Online (Sandbox Code Playgroud)