如何从Windows 8中的服务登录用户

时间:2012-12-10 12:05:22

标签: c# windows-services

我正在开发一个C#windows服务。

我需要知道(来自服务)谁是当前登录的用户。

我尝试使用以下代码跟踪here中的答案:

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

但问题是它只适用于Windows 7,但在Windows 8上我得到一个空字符串。

Windows 8中的WMI是否已更改?

1 个答案:

答案 0 :(得分:0)

问题不在于Windows 8,而是因为我使用了远程连接。 此WMI不适用于远程连接。