C#.Net Windows application - Getting Logged user name in My NT

≯℡__Kan透↙ 提交于 2020-01-14 09:32:10

问题


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


回答1:


//How about this:
string strUserName = WindowsIdentity.GetCurrent().Name;

You can then do whatever you want to do with that "strUserName" variable. Please note it also contains the Domain Name if one is present.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/3252514/c-net-windows-application-getting-logged-user-name-in-my-nt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!