C#.NET Getting User Name of machine using Windows Service

前端 未结 3 1506

I am having difficulty getting the User Name of a person logged into a machine using a windows service.

When using both System.Environment.UserName or WindowsIdentity.Ge

3条回答
  •  -上瘾入骨i
    2021-01-21 04:22

    Try this:

    var connectionOptions = new ConnectionOptions();
    var scope = new System.Management.ManagementScope("\\\\localhost", connectionOptions);
    var query = new System.Management.ObjectQuery("select * from Win32_ComputerSystem");
    var searcher = new ManagementObjectSearcher(scope, query);
    foreach (var row in searcher.Get()) 
    {
        Console.WriteLine(row["UserName"].ToString().ToLower());
    }
    

提交回复
热议问题