Searching the process list

J

JezB

I want to iterate the process list looking at the processes running for the
current user. So I'm using:

foreach (Process p in Process.GetProcesses())
{

}

But I can't see any method or property of the Process object p which tells
me the username of the user running the process. p.StartInfo.UserName always
seems to contain "".

So how can I consider just the processes running under the current user ?
 
W

Willy Denoyette [MVP]

|I want to iterate the process list looking at the processes running for the
| current user. So I'm using:
|
| foreach (Process p in Process.GetProcesses())
| {
|
| }
|
| But I can't see any method or property of the Process object p which tells
| me the username of the user running the process. p.StartInfo.UserName
always
| seems to contain "".
|
| So how can I consider just the processes running under the current user ?
|


You can't use the Process class for this, you will have to use
System.Management and the WMI classes Win32_logonsession and Win32_Process,
or you have to call the Terminal server API's (WTSEnumerateProcesses,
WTSProcessInfo, WTSOpenServer, WTSCloseServer ....) and pass thSID obtained
by WTSProcessInfo to LookupAccountSid using PInvoke.

Willy.
 

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