2013-03-07 63 views
2

我想要獲取任務管理器進程(系統,管理員,網絡服務和本地服務)中所述的所有用戶進程。如何獲取WMI中的所有用戶進程

我需要通過WMI獲取它,我無法找到每個進程的用戶名,並且我檢查了wmi進程和任務管理器進程,wmi僅顯示了管理進程。

你能幫助我得到所有用戶的進程列表?

回答

1

您可以瀏覽Win32_Process class獲取進程的詳細信息:

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); 
    } 
+0

感謝您答覆Arshad,您的解決方案是獲取流程列表,我的問題是獲取所有用戶的流程列表,請幫助我獲取它。 – Gomathipriya 2013-03-09 05:45:09

+0

這個win32_process只顯示管理員用戶的進程列表,我需要獲取所有的用戶進程。 – Gomathipriya 2013-03-09 05:46:16

+0

你檢查過C#'Process'類 – Arshad 2013-03-09 08:24:21

2

也許是有點晚了,但我認爲這是可能使用WMI通過獲取用戶名和域的Win32_Process類的GetOwner()方法(下面的代碼不是我的,我已經從中提取了它:http://social.msdn.microsoft.com/Forums/en-US/d842c407-18f5-478b-8c4f-7e14ac4fbbe6/get-owner-of-curently-runing-procesess):

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 

希望它有助於!

+0

如何獲得與每個進程關聯的應用圖標? – Apoorv 2016-02-24 08:54:57

相關問題