我想获取任务管理器进程(系统、管理员、网络服务和本地服务)中所述的所有用户进程。
我需要通过 WMI 获取它,我找不到每个进程的用户名,我检查了 wmi 进程和任务管理器进程,wmi 仅显示管理员进程。
你能帮我获取所有用户的进程列表吗???
您可以浏览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);
}
也许有点晚了,但我认为可以通过 Win32_Process 类的 GetOwner() 方法使用 WMI,该方法检索用户名和域(下面的代码不是我的,我是从http://social.msdn 中提取的。 microsoft.com/Forums/en-US/d842c407-18f5-478b-8c4f-7e14ac4fbbe6 /get-owner-of-curently-runing-processes ):
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();
}
}
此外,如果您有兴趣,可以使用以下代码使用 vbscript 来确定运行进程的帐户名称(有关更多详细信息,请参见下面的页面http://msdn.microsoft.com/en-us/library/ aa394599(v=vs.85).aspx ):
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
希望能帮助到你!