C# .NET: Get the logged on user on Windows 7 using WMI query
By Perry
Use following code snippet to get the logged on user on Windows 7 using WMI query.
If multiple users logged in then it returns all usernames.
var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT username FROM Win32_ComputerSystem");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("UserName: {0}", queryObj["username"].ToString());
}
Related FAQs
There is a change in security mode from Windows Xp to Windows 7. UAC is one of the changes.
See http://www.eggheadcafe.com/sample-code/Windows7/e53b9d81-c025-4e41-a45c-f22214221c53/windows-7-uac-user-accou.aspx for UAC concept details. This shows how to disable it.
C# .NET: Get the logged on user on Windows 7 using WMI query (1113 Views)