How to identify user, who runs a certain process?
-
I use the Process.GetProcessesByName-Method to retreive all processes, having the same name. In certain circumstances this processes run under different users. How can I find out which user runs which process? Completely different methods than using System.Diagnostics.Process are also appreciated. Thanks.
-
I use the Process.GetProcessesByName-Method to retreive all processes, having the same name. In certain circumstances this processes run under different users. How can I find out which user runs which process? Completely different methods than using System.Diagnostics.Process are also appreciated. Thanks.
You can get this info by using the System.Management namespace functions. I do not have an example in c#, but I have one in vb.net if you are interested. Basically: System.Management.ConnectionOptions con = new System.Management.ConnectionOptions; System.Management.ManagementScope mgmtScope = new System.Management.ManagementScope("\\localhost", con); System.Management.ObjectQuery objQuery = new System.Management.WqlObjectQuery("SELECT * FROM win32_process"); System.Management.ManagementObjectSearcher objSearch = new Management.ManagementObjectSearcher(mgmtScope, objQuery); System.Management.ManagementObjectCollection objCol = objSearch.Get; Object obj; //Loop through all the processes in objCol to find the one you want. foreach(System.Management.Object objProc in objCol) { //if the obj is the proc you are after obj = objProc.InvokeMethod("GetOwner",info); //where info is an array look at docs //I think info[0] is domain and info[1] is user } This example code was cobbled together in the message reply editor, and I do not use c# enough to say that these are valid statements, but if you read the documentation on the System.Management objects used, you should be able to figure it out. Scott Serl