WMI: Determine Running Tasks

G

Guest

Using WMI, how do I retrieve the list of running applications on a pc? If
you open up task manager and click on the applications tab, I am trying to
retrieve the task name and the status. I looked high and low in the WMI
documentation and unable to find a class that returns this info.

Any help is appreciated.

#region List all properties from WMI Query
// WMI Documentation
//
http://msdn.microsoft.com/library/d...isdk/wmi/computer_system_hardware_classes.asp


string wmiQuery = "Select * from Win32_Process";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
Console.WriteLine("================================================");
Console.WriteLine(wmiQuery);
Console.WriteLine("================================================");
foreach (ManagementObject service in searcher.Get())
{
Console.WriteLine("================================================");
Console.WriteLine("SERVICE: " + service.ToString());
Console.WriteLine("================================================");
foreach ( PropertyData s in service.Properties)
{
Console.WriteLine("Name: {0} Value: {1}", s.Name.ToString(),
service[s.Name.ToString()]);
}
Console.ReadLine();

}
Console.WriteLine("================================================");
Console.WriteLine("PRESS ENTER TO EXIT");
Console.WriteLine("================================================");
Console.ReadLine();
#endregion
 
M

Matt

Chris said:
Using WMI, how do I retrieve the list of running applications on a pc? If
you open up task manager and click on the applications tab, I am trying to
retrieve the task name and the status. I looked high and low in the WMI
documentation and unable to find a class that returns this info.

Technically, you can't. However, if you are trying to reproduce task
manager,
you might try something like this:

Process[] myProcesses = Process.GetProcesses();
int idx = 0;
foreach(Process myProcess in myProcesses)
{
if ( myProcess.MainWindowHandle != IntPtr.Zero )
{
Console.WriteLine(myProcess.MainWindowTitle);
idx ++;
}
}
Console.WriteLine("{0} Processes found\n", idx );

Basically, I'm looking at any process that has a main window. This
won't
work 100% of the time, but I think it works well enough to be useful.

Matt
 
G

Guest

Hi Matt,

This should get me by. Thanks.

Matt said:
Chris said:
Using WMI, how do I retrieve the list of running applications on a pc? If
you open up task manager and click on the applications tab, I am trying to
retrieve the task name and the status. I looked high and low in the WMI
documentation and unable to find a class that returns this info.

Technically, you can't. However, if you are trying to reproduce task
manager,
you might try something like this:

Process[] myProcesses = Process.GetProcesses();
int idx = 0;
foreach(Process myProcess in myProcesses)
{
if ( myProcess.MainWindowHandle != IntPtr.Zero )
{
Console.WriteLine(myProcess.MainWindowTitle);
idx ++;
}
}
Console.WriteLine("{0} Processes found\n", idx );

Basically, I'm looking at any process that has a main window. This
won't
work 100% of the time, but I think it works well enough to be useful.

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top