简体   繁体   中英

How to get logged in user from a service in windows 8

I am developing a C# windows service.

I need to know (from the service) who is the currently logged on user.

I tried to follow the answer in here with this code:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();
string username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];

But the problem is that it works only on Windows 7, but on Windows 8 I get an empty string.

Was the WMI changed in Windows 8?

The problem wasn't with windows 8 but the fact that I used remote connection. This WMI doesn't work for remote connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM