I am looking for a process by the name of "MyApp.exe" and I want to make sure I get the process that is owned by a particular user.
I use the following code to get a list of the processes:
Process[] processes = Process.GetProcessesByName("MyApp");
This gives me a list of processes, but there does not appear to be a way in the Process class to determine who owns that process? Any thoughts on how I can do this?
Unfortunately there's no native .Net way of getting the process owner.
Have a look at these for a potential solution:
This is the easiest way I've found to do it:
Since WMI is not always a fast way of retrieving information, here is the native P/Invoke way of doing it:
The return value is
null
when unsuccessful. In order to get the names of processes running under the SYSTEM user, you need to execute this code as administrator.You can use WMI to get the user owning a certain process. To use WMI you need to add a reference to the
System.Management.dll
to your project.By process id:
By process name (finds the first process only, adjust accordingly):
Here is the VB version for the non C# speakers: