杀死Windows资源管理器的问题?

问题描述 投票:5回答:5

我需要杀死Windows资源管理器的进程(explorer.exe)

让我说我使用本机NT方法TerminateProcess

它有效,但问题是探险家再次启动,无论如何,可能是Windows正在这样做。当我用Windows任务管理器杀死explorer.exe时,它不会回来,它会被杀死。

我想做任务管理员通过我的申请做的事情。

编辑: 感谢@sblom我解决了它,在注册表中快速调整就可以了。虽然它是一个聪明的黑客,显然有任务的人有一个更干净的方式,这就是说,我决定现在用@ sblom的方式。

c# windows process explorer
5个回答
8
投票

“真正的”解决方案。 (完整的程序。经测试可在Windows 7上运行。)

using System;
using System.Runtime.InteropServices;

namespace ExplorerZap
{
    class Program
    {
        [DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);

        static void Main(string[] args)
        {
            int hwnd;
            hwnd = FindWindow("Progman", null);
            PostMessage(hwnd, /*WM_QUIT*/ 0x12, 0, 0);
            return;
        }
    }
}

14
投票

来自Technet

您可以将注册表项HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoRestartShell设置为0,它将不再自动重新启动。


5
投票

这是另一个解决这个问题的方法 - 而是api调用它使用windows附带的外部工具(至少Win 7 Professional):

    public static class Extensions
    {
        public static void ForceKill(this Process process)
        {
            using (Process killer = new Process())
            {
                killer.StartInfo.FileName = "taskkill";
                killer.StartInfo.Arguments = string.Format("/f /PID {0}", process.Id);
                killer.StartInfo.CreateNoWindow = true;
                killer.StartInfo.UseShellExecute = false;
                killer.Start();
                killer.WaitForExit();
                if (killer.ExitCode != 0)
                {
                    throw new Win32Exception(killer.ExitCode);
                }
            }
        }
    }

我知道Win32Exception可能不是最好的Exception,但是这个方法或多或少像Kill一样 - 除了它实际上杀死了Windows资源管理器。

我已将其添加为扩展方法,因此您可以直接在Process对象上使用它:

    foreach (Process process in Process.GetProcessesByName("explorer"))
    {
        process.ForceKill();
    }

您必须首先确保taskkill工具在生产环境中可用(似乎已经有一段时间使用windows:https://web.archive.org/web/20171016213040/http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/taskkill.mspx?mfr=true)。

编辑:原始链接死亡,取而代之的是来自Internet Archive Wayback Machine的缓存。有关Windows 2012/2016的更新文档,请访问:https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill


2
投票

您可能需要做的是不使用TerminateProcess,而是将WM_QUIT消息发布到资源管理器窗口和主线程。它有点涉及,但我发现这个页面有一些示例代码可以帮助你:

http://www.replicator.org/node/100

Windows将在TerminateProcess之后自动重启explorer.exe,以便在崩溃终止时重新启动。


0
投票

我有一些研究,这些是结果:

Windows将在关闭后重新启动explorer - 除非由任务管理器 - 。

所以你应该改变相关的RegistryKey

RegistryKey regKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default).OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\Winlogon", RegistryKeyPermissionCheck.ReadWriteSubTree);
if (regKey.GetValue("AutoRestartShell").ToString() == "1")
    regKey.SetValue("AutoRestartShell", 0, RegistryValueKind.DWord);

要更改注册表项,程序应以管理员身份运行:

  • 您可以向用户显示UAC提示,以管理员身份运行应用程序,并在此Answer中进行解释。如果UAC被关闭,我会引导你到这个Answer
  • 您可以在exe中嵌入清单文件,这将导致Windows 7始终以管理员身份运行程序,正如此Answer中所述。
  • 你应该知道你不能强迫你的进程以管理员身份启动;所以你可以在你的流程中运行你的流程作为另一个流程!你可以使用这个blog post或这个answer
  • 您还可以将reg命令与此[Microsoft Windows文档] .6一起使用。

设置-restarting explorer- off后:此代码可以关闭explorer

Process[] ps = Process.GetProcessesByName("explorer");
foreach (Process p in ps)
    p.Kill();
© www.soinside.com 2019 - 2024. All rights reserved.