C#.Net Windows application - Getting Logged user name in My NT
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.
开发者_如何学CThanks
//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.
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
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
精彩评论