Thursday, July 30, 2009

UAC Compatible applications - Part 3 ( Getting Process List )

So now we have got an idea of how to get our application work in Vista. There are few tips which could help us tackle minor issues which we might face in Vista.

One of such issues is when we have to get the list of all processes active in the system. What we usually do is call the System.Diagnostics.Process.GetProcesses() method. This, however, get us into trouble when running from limited user in vista. The Limited User in vista doesn’t have privilege to fetch this list.

Of course, it is not end of the world and is definitely, there is one work-around for this as well.

You could query the WMI Classes to get the desired result.

ManagementScope scope = new ManagementScope();
System.Management.ObjectQuery query = new ObjectQuery("select * from Win32_Service");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);


foreach(ManagementBaseObject objService in searcher.Get())
{
Console.WriteLine(objService["Name"]);
}



Simple as it can get.