windows 杀死Windows资源管理器的问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2570244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem with Killing windows explorer?
提问by Vivek Bernard
I need to kill windows explorer's process (explorer.exe), for that
为此,我需要终止 Windows 资源管理器的进程 (explorer.exe)
lets say i use a native NT method TerminateProcess
假设我使用本地 NT 方法 TerminateProcess
It works but the problem is that the explorer starts again, may be windows is doing that, anyway. When i kill explorer.exe with windows task manager, it doesn't come back, its stays killed.
它有效,但问题是资源管理器再次启动,可能是 Windows 正在这样做,无论如何。当我用 Windows 任务管理器杀死 explorer.exe 时,它不会回来,它会一直被杀死。
I want to do whatever taskmanager is doing through my application.
我想通过我的应用程序做任务管理器正在做的任何事情。
Edit:
Thanks to @sblom i solved it, a quick tweak in the registry did the trick. Although its a clever hack, apparently taskmnager has a cleaner way of doing that, that said, i've decided to go with @sblom's way for now.
编辑:
感谢@sblom,我解决了这个问题,注册表中的一个快速调整就成功了。虽然它是一个聪明的黑客,但显然 taskmnager 有一种更干净的方法来做到这一点,也就是说,我现在决定采用@sblom 的方式。
回答by sblom
回答by sblom
The "real" solution. (Complete program. Tested to work on Windows 7.)
“真正的”解决方案。(完整的程序。经测试可在 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;
}
}
}
回答by marchewek
Here's another solution to this problem - instead api calls it uses an external tool shipped with windows (at least Win 7 Professional):
这是此问题的另一种解决方案 - 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);
}
}
}
}
I know that Win32Exception may not be the best Exception, but this method acts more or less like Kill - with the exception that it actually kills windows explorer.
我知道 Win32Exception 可能不是最好的 Exception,但这种方法的行为或多或少类似于 Kill——除了它实际上杀死了 Windows 资源管理器。
I've added it as an extension method, so you can use it directly on Process object:
我已将其添加为扩展方法,因此您可以直接在 Process 对象上使用它:
foreach (Process process in Process.GetProcessesByName("explorer"))
{
process.ForceKill();
}
You must first ensure that the taskkill tool is available on production environment (it seems that it's been for a while with windows: https://web.archive.org/web/20171016213040/http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/taskkill.mspx?mfr=true).
你必须首先确保taskkill工具在生产环境中可用(在windows上似乎已经有一段时间了:https: //web.archive.org/web/20171016213040/http: //www.microsoft.com/resources /documentation/windows/xp/all/proddocs/en-us/taskkill.mspx?mfr=true)。
EDIT:Original link dead, replaced with cache from Internet Archive Wayback Machine. Updated documentation for Windows 2012/2016 can be found at: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill
编辑:原始链接已失效,替换为 Internet Archive Wayback Machine 中的缓存。可在以下位置找到 Windows 2012/2016 的更新文档:https: //docs.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill
回答by Gerald
What you probably need to do is instead of using TerminateProcess, post a WM_QUIT message to the explorer windows and main thread. It's a bit involved, but I found this page which has some example code that might help you along:
您可能需要做的不是使用 TerminateProcess,而是将 WM_QUIT 消息发布到资源管理器窗口和主线程。这有点复杂,但我发现这个页面有一些示例代码可以帮助你:
http://www.replicator.org/node/100
http://www.replicator.org/node/100
Windows will automatically restart explorer.exe after a TerminateProcess so that it restarts in the case of a crash termination.
Windows 将在 TerminateProcess 之后自动重新启动 explorer.exe,以便在崩溃终止的情况下重新启动。
回答by shA.t
I have some researches and these are reslts:
我有一些研究,这些是结果:
Windows will restart
explorer
after it closed -except by Task Manager-.
Windows 将
explorer
在关闭后重新启动-任务管理器除外-。
So you should change the related RegistryKey
:
所以你应该改变相关的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);
For changing a registry key the program should run as administrator:
要更改注册表项,程序应以管理员身份运行:
- You can show UAC prompt to user to run application as administrator as explaining in this Answer. And if UAC is turned off I direct you to this Answer.
- You can embed a manifest file in the
exe
, which will cause Windows Seven to always run the program as an administrator, As explaining in this Answer. - You should know you can't force your process starts as administrator; so you can run your process inside your process as another process! You can use this blog postor this answer.
- You can also use
reg
command with this [Microsoft Windows Documentation].6.
- 您可以向用户显示 UAC 提示以管理员身份运行应用程序,如本答案中所述。如果 UAC 已关闭,我会将您定向到此答案。
- 您可以嵌入在一个清单文件
exe
,这将导致Windows 7中始终运行该程序作为管理员,在本解释回答。 - 你应该知道你不能强制你的进程以管理员身份启动;所以你可以在你的进程中运行你的进程作为另一个进程!您可以使用此博客文章或此答案。
- 您还可以
reg
在此 [Microsoft Windows 文档] 中使用命令。6.
After setting that -restarting explorer- off: This code can close explorer
:
设置后 -重新启动资源管理器- 关闭:此代码可以关闭explorer
:
Process[] ps = Process.GetProcessesByName("explorer");
foreach (Process p in ps)
p.Kill();