"Noah Coad [MVP .NET/C#]" <no**@coad.net> wrote in message
news:us*************@TK2MSFTNGP12.phx.gbl...
Howdy! I need to know what user is using which process on the system.
"System.Diagnostics.Process.GetProcesseses" will return an array of
processes, but I need to know if the process returned is owned by the
current user or other users on the system (Windows 2003 Server). How is
this accomplished?
Thanks!
- Noah Coad -
Microsoft MVP
Or you can use the System.Management classes (and WMI), or you can call the
TS API's (WTSEnumerateProcesses and friends) using PInvoke.
Here's how to use the former.
using System.Management;
class Sample_SelectQuery
{
public static void Main() {
SelectQuery selectQuery = new SelectQuery("Win32_Process");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(selectQuery);
foreach (ManagementObject proc in searcher.Get()) {
Console.WriteLine(proc["Name"].ToString());
//out argument to return user and domain
string[] s = new String[2];
//Invoke the method and populate the array with the user name
and domain
proc.InvokeMethod("GetOwner",(object[])s);
Console.WriteLine("User: " + s[1]+ "\\" + s[0]);
}
}
Willy.