views:

50

answers:

2

I need to get the username who logged in particular machine in my LAN. Suggest me a best method to get the user name by passing machine name in C#.net windows application. Also consider the permission.

Thanks

A: 

As I understand it, you want to remotely determine the username of people logged on to many PCs and present the results in a Windows Forms application.

Windows does not have a built-in mechanism to enumerate this information.

Whichever mechanism you ultimately choose to use, you will probably need to run the scanning application under a user account which has admin rights on the PC being scanned.

You might choose to emulate the behaviour of the SysInternals command PsLoggedOn which examines the HKEY_USERS key on the remote computer. To find out who is connected to a PC (i.e. accessing shares), use the NetSessionEnum API.

More information about PsLoggedOn can be found here: link text

IanT8
yes i want the second one "Do you want to remotely determine the username of people logged on to many PCs and present the results in a Windows Forms application? "
Ksmps
A: 

Hi All I got the solution for my question. I used WMI to get the userName.

try
{
    object[] objArr = new object[2];
    ManagementScope ms = new ManagementScope("Path");
    ms.Connect();
    if (ms.IsConnected)
    {
        ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_Process WHERE Name='explorer.exe'");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, Query);
        foreach (ManagementObject objQuery in searcher.Get())
        {
            objQuery.InvokeMethod("GetOwner", objArr); // objArr[0] contains the userId and objArr[1] contains Domainname
            userName = Convert.ToString(objArr[0]);
        }
    }
    catch (System.Exception ex)
    {
        throw ex;
    }

Thanks

Ksmps