tags:

views:

73

answers:

0

Using WMI how can I get the last logged in user for a computer. Currently this is what I have for some reason its returning null. Any ideas why and is this the correct way to get the last logged in user. (NOTE: I am using it on Windows 7 machines)

Using System.Management;

var scope = new ManagementScope(@"\\COMPUTER_NAME\root\CIMV2");
var query = new SelectQuery("Win32_ComputerSystem");
var searcher = new ManagementObjectSearcher(scope, query);
foreach (ManagementObject result in searcher.Get())
{
     Console.WriteLine("Last Logged In User: " + (string)result["UserName"]);
}