简体   繁体   English

在 C# 中获取管理员进程

[英]Get administrator processes in C#

I am trying to print all running processes, similarly to how tasklist or ps does in C#.我正在尝试打印所有正在运行的进程,类似于tasklistps在 C# 中的作用。 I tried it with the following code:我用下面的代码试了一下:

foreach(Process process in Process.GetProcesses()) {
    if(!process.HasExited) {
        Console.WriteLine(process.ProcessName);
    }
}

But with it, I run into some issues on Windows (did not try it on Linux):但是有了它,我在 Windows 上遇到了一些问题(没有在 Linux 上尝试过):

  • When I execute the above code with user-privileges, it only prints processes started by this user.当我使用用户权限执行上述代码时,它只打印由该用户启动的进程。 If I run it as administrator, all processes are printed如果我以管理员身份运行它,则打印所有进程
  • But when I open a cmd window with user privileges and list the processes (ie execute tasklist ), all processes are printed, even those started by the system.但是当我用用户权限打开一个 cmd 窗口并列出进程(即执行tasklist )时,所有进程都会被打印出来,即使是那些由系统启动的进程。

Is there a better way to get all processes (without requiring the program to be run as administrator)?有没有更好的方法来获取所有进程(不需要以管理员身份运行程序)?

One solution I am really trying to avoid would be something along the lines of:我真正想避免的一种解决方案是:

System.Diagnostics.Process.Start("CMD.exe", "tasklist > C:\file.txt");
String[] processes = File.ReadAllLines(@"C:\file.txt");

You can't.你不能。 You can't let the same process run parts in elevated and in user mode.您不能让同一进程在提升和用户模式下运行部件。 That's just the way they built it, mainly for security reasons.这就是他们构建它的方式,主要是出于安全原因。 You can't just bypass it.你不能绕过它。

Enough about what you can't.关于你不能做的事情已经足够了。 What can you do?你能做什么? You could start a second (different) program that runs in elevated mode, or you could restart your current application when there is a section you need elevated privileges.您可以启动在提升模式下运行的第二个(不同)程序,或者您可以在有需要提升权限的部分时重新启动当前应用程序。 You need Process.Start for this and you will have to set the ProcessStartInfo.Verb to "runas" :您需要Process.Start为此,您必须将ProcessStartInfo.Verb设置为"runas"

ProcessStartInfo startInfo = new ProcessStartInfo(exePath);
startInfo.Verb = "runas";

Process.Start(startInfo);

After some testing I found out, that administrator processes will show in the Process.GetProcesses() list but the boolean HasExited of admin privileged processes is always true (even if it hasn't closed)经过一些测试,我发现管理员进程显示在Process.GetProcesses()列表中,管理员特权进程的布尔值 HasExited 始终为真(即使它尚未关闭)

So I just looped through the list without checking for process.HasExited所以我只是遍历列表而不检查process.HasExited

foreach(Process process in Process.GetProcesses()) {
    if(!process.HasExited) { //running process with user privileges
        //do some stuff here
    }
    else { //closed process or administrator process
        //do some stuff here
    }
}

And for the case that the process has actually stopped, I wrap it inside a try-catch block.对于进程实际上已经停止的情况,我将它包装在一个try-catch块中。

As this solution does not seem optimal to me, I am leaving this question open.由于此解决方案对我来说似乎不是最佳选择,因此我将这个问题悬而未决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM