Viv*_*ard 5 c# windows explorer process
我需要杀死Windows资源管理器的进程(explorer.exe)
让我说我使用本机NT方法TerminateProcess
它的工作原理,但问题是探险家再次启动,无论如何,可能是Windows正在这样做.当我用Windows任务管理器杀死explorer.exe时,它不会回来,它会被杀死.
我想做任务管理员通过我的申请做的事情.
编辑:
感谢@sblom我解决了它,在注册表中快速调整诀窍.虽然它是一个聪明的黑客,显然有任务的人有一个更干净的方式,这就是说,我决定现在用@ sblom的方式.
"真正的"解决方案.(完整的程序.经测试可在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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是另一个解决这个问题的方法 - 而是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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道Win32Exception可能不是最好的Exception,但是这个方法或多或少像Kill一样 - 除了它实际上杀死了Windows资源管理器.
我已将其添加为扩展方法,因此您可以直接在Process对象上使用它:
foreach (Process process in Process.GetProcessesByName("explorer"))
{
process.ForceKill();
}
Run Code Online (Sandbox Code Playgroud)
您必须首先确保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
| 归档时间: |
|
| 查看次数: |
6170 次 |
| 最近记录: |