我希望获取所有用户的进程列表,就像任务管理器中显示的进程(系统、管理员、网络服务和本地服务)。
我需要通过WMI获取它,但是我无法找到每个进程的用户名,并且我已经检查了WMI进程和任务管理器进程,WMI仅显示管理员进程。
你能帮我获取所有用户的进程列表吗?
using System;
using System.Diagnostics;
using System.Management; // Add reference to System.Management!!
class Program {
static void Main(string[] args) {
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_Process");
foreach (ManagementObject queryObj in searcher.Get()) {
ManagementBaseObject outParams =
queryObj.InvokeMethod("GetOwner", null, null);
Console.WriteLine("{0} owned by {1}\\{2}", queryObj["Name"],
outParams["Domain"], outParams["User"]);
}
Console.ReadLine();
}
}
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process")
For Each objProcess in colProcessList
colProperties = objProcess.GetOwner( _
strNameOfUser,strUserDomain)
Wscript.Echo "Process " & objProcess.Name _
& " is owned by " _
& strUserDomain & "\" & strNameOfUser & "."
Next
您可以浏览Win32_Process类来获取进程的详细信息:
using System;
using System.Management;
using System.Windows.Forms;
namespace WMISample
{
public class MyWMIQuery
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_Process");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_Process instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("Caption: {0}", queryObj["Caption"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
}
}
}
或者在C#中,不使用WMI
:
Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist)
{
Console.WriteLine(“Process: {0} ID: {1}”, theprocess.ProcessName, theprocess.Id);
}
Process
类? - Arshad