2009-06-15 12 views
0

ログオンしているユーザーの一覧を取得する方法を知りたいと思います。私はWindowsのタスクマネージャーを開き、[ユーザー]タブを見て、PCにログオンしているユーザーを調べることができます。そのユーザーリストを取得するにはどうすればいいですか?ありがとう。ログオンセッションの一覧

答えて

1

WMIをご覧ください。

System.Management.ConnectionOptions myConnectionOptions = new System.Management.ConnectionOptions(); 
     myConnectionOptions.Impersonation = System.Management.ImpersonationLevel.Impersonate; 

    System.Management.ManagementScope objwmiservice; 
    System.Management.ManagementObjectSearcher myObjectSearcher; 
    System.Management.ManagementObjectCollection myCollection; 

    try 
    { 
     objwmiservice = new System.Management.ManagementScope(("\\\\" + (HostOrIP + "\\root\\cimv2")), myConnectionOptions); 
     objwmiservice.Connect(); 
     myObjectSearcher = new System.Management.ManagementObjectSearcher(objwmiservice.Path.ToString(), "Select UserName from Win32_ComputerSystem"); 
     myObjectSearcher.Options.Timeout = new TimeSpan(0, 0, 0, 0, 7000); 
     myCollection = myObjectSearcher.Get(); 

     foreach (System.Management.ManagementObject myObject in myCollection) 
     { 
      if (!(myObject.GetPropertyValue("User name") == null)) 
      { 
       string Userx = myObject.GetPropertyValue("Usernam e").ToString(); 
       int posx = Userx.LastIndexOf("\\"); 
       if ((posx > 0)) 
       { 
        Userx = Userx.Substring((posx + 1)); 
        return Userx.ToUpper(); 
       } 
      } 
     } 
     return "<Nobody>"; 
    } 
    catch (Exception) 
    { 
      return "<Nobody>"; 
    } 
} 

私はthis link

+0

感謝から得た:あなたは、System.Management名前空間を使用することができます。高速のユーザー切り替えを使用して複数のログインを行っている場合、これは1人のユーザーしか返されず、そのユーザーがアクティブになります。 – James