I have been looking for a way to find the username associated with a specified process on widows 7 and above (if you open up task manager in the processes tab i want to provide image name to get back user name). This is not necessarily the current user.
Through my online search i found the following solution crop up a few times and it does pretty much exactly what i want:
Public Shared Sub Main()
Dim selectQuery As SelectQuery = New SelectQuery("Win32_Process")
Dim searcher As ManagementObjectSearcher = New
ManagementObjectSearcher(selectQuery)
For Each proc As ManagementObject In searcher.Get
Console.WriteLine(proc("Name").ToString)
Dim s(1) As String
proc.InvokeMethod("GetOwner", CType(s, Object()))
Console.WriteLine(("User: " & (s(1) + ("\\" + s(0)))))
Next
Console.ReadLine()
End Sub
The only issue i have is that this code only works for framework 4.5 and up. the application i'm currently developing is only 4.0 (not my choice unfortunately).
Does anyone have a framework 4.0 solution to this problem?
Thanks
Edit: OK, so it turns out I've been a bit of an idiot, everything used in the method above is supported in .net 4.0 (i forgot you have to use the drop down on MSDN to see the older supported versions).... i have added the system.management imports to my class however it does not seem to be recognizing the SelectQuery and ManagementObjectSearcher types... does anyone know how to solve this?