Getting the name of user on remote computer

H

Heike Pertzel

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
}
}
******************************************************************************************************
 
W

Willy Denoyette [MVP]

Heike Pertzel said:
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.
 
H

Heike Pertzel

Hallo Willy,



thank you for your answer.

I've got a solution now that works in some cases. I'm testing along. If I
need your help again I'll start a new question.



Heike Pertzel





Willy Denoyette said:
Heike Pertzel said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top