1

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?

Community
  • 1
  • 1
user844541
  • 2,868
  • 5
  • 32
  • 60
  • 1
    Have you tried inspecting the overall contents of `searcher` ? I suspect it has something to do with logging in through a Microsoft Account. – Alex Dec 10 '12 at 12:09

1 Answers1

0

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

user844541
  • 2,868
  • 5
  • 32
  • 60