Hallo NG!
I would like to get the name of the user logged on on a another computer.
I've written the following code which gives me the LogonId of this user.
But getting with this Id the name of the user doesn't work.
I would appreciate every hint or help!
Regards
Heike Pertzel
*************************************************************************** ***************************
using System.Management;
string UserLogonId ;
string Username;
ManagementScope msc = new ManagementScope("\\\\" + IP of the OTHER computer
+ "\\root\\cimv2");
string queryString = "select LogonId from win32_logonsession where logontype
= 2";
query = new ManagementObjectSearcher(msc, new SelectQuery(queryString));
foreach( ManagementObject mo in query.Get())
{
UserLogonId = mo["LogonId"].ToString(); // WORKS !!!!!!
RelatedObjectQuery relatedQuery = new RelatedObjectQuery("associators of
{Win32_LogonSession.LogonId='" + mo["LogonId"]+ "'}WHERE AssocClass =
Win32_LoggedOnUser");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(msc,
relatedQuery);
foreach (ManagementObject mob in searcher.Get())
{
Username = mob["Caption"] + " " + mob["Domain"] + " " + mob["Name"];
// DOESN'T WORK !!!!!!! they are always empty strings
}
}
*************************************************************************** ***************************
"Heike Pertzel" <HPN
@DATA5.de> wrote in message
news:%235lHwvAqHHA.4608@TK2MSFTNGP02.phx.gbl...
> Hallo NG!
> I would like to get the name of the user logged on on a another computer.
> I've written the following code which gives me the LogonId of this user.
> But getting with this Id the name of the user doesn't work.
> I would appreciate every hint or help!
> Regards
> Heike Pertzel
> *************************************************************************** ***************************
> using System.Management;
> string UserLogonId ;
> string Username;
> ManagementScope msc = new ManagementScope("\\\\" + IP of the OTHER
> computer + "\\root\\cimv2");
> string queryString = "select LogonId from win32_logonsession where
> logontype = 2";
> query = new ManagementObjectSearcher(msc, new SelectQuery(queryString));
> foreach( ManagementObject mo in query.Get())
> {
> UserLogonId = mo["LogonId"].ToString(); // WORKS !!!!!!
> RelatedObjectQuery relatedQuery = new RelatedObjectQuery("associators
> of {Win32_LogonSession.LogonId='" + mo["LogonId"]+ "'}WHERE AssocClass =
> Win32_LoggedOnUser");
> ManagementObjectSearcher searcher = new ManagementObjectSearcher(msc,
> relatedQuery);
> foreach (ManagementObject mob in searcher.Get())
> {
> Username = mob["Caption"] + " " + mob["Domain"] + " " +
> mob["Name"]; // DOESN'T WORK !!!!!!! they are always empty strings
> }
> }
> *************************************************************************** ***************************
This should work, provided the remote system runs XP or higher.
Willy.