C# Process.Kill() 访问被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18129527/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:23:25  来源:igfitidea点击:

Process.Kill() Access Denied

c#.netprocesskill

提问by Ezzy

When I run the following code, a Win32Exception is thrown for Access Denied. I cannot find any solutions via search. How do I fix this?

当我运行以下代码时,会为拒绝访问引发 Win32Exception。我无法通过搜索找到任何解决方案。我该如何解决?

foreach (ListViewItem list in showprocesses.SelectedItems)
{
    Process p = System.Diagnostics.Process.GetProcessById(Convert.ToInt32(list.Tag));
    if (p != null)
        p.Kill();
}

回答by Jason

You will generally get this error if you do not have the necessary permissions. You must be an administrator, and in win vista and above, run your app/process in elevated mode. Furthermore, there are certain processes that even as admin you won't be able to kill, some deemed system critical, etc, and you may need to run as system, and then there are those that even system can't kill, like antivirus, or an actual virus, because they don't want you killing their process

如果您没有必要的权限,通常会出现此错误。您必须是管理员,并且在 win vista 及更高版本中,以提升模式运行您的应用程序/进程。此外,有些进程即使作为管理员你也无法杀死,一些被认为是系统关键的进程,等等,你可能需要作为system运行,然后还有那些甚至系统也无法杀死的进程,例如防病毒软件,或者一个真正的病毒,因为他们不想让你杀死他们的进程

Another possibility is that if the process is already terminating, it will also throw that exception, see MSDN

另一种可能性是,如果进程已经终止,它也会抛出该异常,参见MSDN

回答by Henry Kerval

I had this kind of problems with a Delphi application Under Windows 8.1 My application was closing, but was still in the background processes of the task manager. Impossible to kill it with TaskKill (tried admin mode, "/F" option, from command line...) Finally I found out that Windows "marked" a DLL of my application as "IgnoreFreeLibrary". That is why my application was not closing. Here is an extract of the registry :

我在 Windows 8.1 下的 Delphi 应用程序中遇到了这种问题 我的应用程序正在关闭,但仍在任务管理器的后台进程中。不可能用 TaskKill 杀死它(尝试管理模式,“/F”选项,从命令行......)最后我发现 Windows 将我的应用程序的 DLL“标记”为“IgnoreFreeLibrary”。这就是我的申请没有关闭的原因。这是注册表的摘录:

[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"{MyApplicationPathAndExeName}"="$ IgnoreFreeLibrary<DllWithProblemName.Dll>"

I erased the registry entry and everything was back to normal.

我删除了注册表项,一切都恢复正常。

回答by Hamed R

I had same problem and used these codes to solve the problem:

我遇到了同样的问题并使用这些代码来解决问题:

    [DllImport("user32.dll")]
    public static extern int FindWindow(string ClassName, string WindowName);

    [DllImport("user32.dll")]
    public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

    public const int WM_SYSCOMMAND = 0x0112;
    public const int SC_CLOSE = 0xF060;

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        int HWND = FindWindow(null, "My Window");//window title

        SendMessage(HWND, WM_SYSCOMMAND, SC_CLOSE, 0);
    }

回答by abd Radwan

disable UAC on windows solve the problem.

在 windows 上禁用 UAC 解决了这个问题。

回答by Rachitha Jewandara

try { pyProcess.Kill(); } catch (Exception error) { Console.WriteLine(error.ToString()); }

尝试 { pyProcess.Kill(); } catch (Exception error) { Console.WriteLine(error.ToString()); }

No need for Admin Access. But this will work only some process only.

无需管理员访问。但这只会在某些过程中起作用。