Get logged in user name in Remote System in C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
HI All,

i need to get logged user in a remote machine connected to a network.
i searched in web and tried so many methods. but didnt succeded. with the below code for some system im getting the username.
but for some systems im not getting.
can any body help how can i get User name in remote system???
My Code :

<span style="font-size:small <font size="3" style="font-size:small

</font><span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small public<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small static<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small string<span style="font-size:small whoisLoggedIn(<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small string<font size="3
strComputer)</font><span style="font-size:small
{
<font size="3" style="font-size:small

</font><font size="3" color="#0000ff <font size="3" color="#0000ff try</font></font><span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small <span style="font-size:small
{
<font size="3" style="font-size:small

</font><span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ConnectionOptions<span style="font-size:small connection =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small new<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ConnectionOptions<font size="3 ();</font><span style="font-size:small <font size="3" style="font-size:small

</font><span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ManagementScope<span style="font-size:small scope =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small new<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ManagementScope<span style="font-size:small (<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "\\"<span style="font-size:small
+ strComputer + <span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "\root\CIMV2"<font size="3 , connection);</font><span style="font-size:small
scope.Connect();
<font size="3" style="font-size:small

</font><span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ObjectQuery<span style="font-size:small query =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small new<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ObjectQuery<span style="font-size:small (<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "SELECT
* FROM Win32_Process WHERE Name = explorer.exe"<font size="3 );</font><span style="font-size:small <font size="3" style="font-size:small

</font><span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ManagementObjectSearcher<span style="font-size:small searcher =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small new<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ManagementObjectSearcher<font size="3 (scope, query);</font><span style="font-size:small <font size="3" style="font-size:small

</font><span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small foreach<span style="font-size:small (<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ManagementObject<span style="font-size:small
queryObj <span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small in<font size="3 searcher.Get())</font><span style="font-size:small
{
<font size="3" style="font-size:small

</font><span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ManagementPath<span style="font-size:small path =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small new<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ManagementPath<span style="font-size:small (<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Win32_Process.Handle="<span style="font-size:small
+ queryObj[<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Handle"<span style="font-size:small ] +
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small ""<font size="3 );</font><span style="font-size:small <font size="3" style="font-size:small

</font><span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ManagementObject<span style="font-size:small classInstance =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small new<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ManagementObject<span style="font-size:small (scope, path,
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small null<font size="3 );</font><span style="font-size:small <font size="3" style="font-size:small

</font><span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ManagementBaseObject<span style="font-size:small outParams = classInstance.InvokeMethod(<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "GetOwner"<span style="font-size:small ,
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small null<span style="font-size:small ,
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small null<font size="3 );</font><span style="font-size:small <font size="3" style="font-size:small

</font><span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small return<span style="font-size:small (outParams[<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "User"<font size="3 ].ToString());</font><span style="font-size:small
}
}
<font size="3" style="font-size:small

</font><span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small catch<span style="font-size:small (<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small ManagementException<font size="3
err)</font><span style="font-size:small
{
<font size="3" style="font-size:small

</font><span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small return<span style="font-size:small (<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "NA"<font size="3 );</font><span style="font-size:small
}
<font size="3" style="font-size:small

</font><span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small catch<span style="font-size:small (System.<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small UnauthorizedAccessException<font size="3
unauthorizedErr)</font><span style="font-size:small
{
<font size="3" style="font-size:small

</font><span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small return<span style="font-size:small (<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "NA"<font size="3 );</font><span style="font-size:small
}
<font size="3" style="font-size:small

</font><span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small catch<span style="font-size:small (<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Exception<font size="3
ex)</font><span style="font-size:small
{
<font size="3" style="font-size:small

</font><span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small return<span style="font-size:small (<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "NA"<font size="3 );</font><span style="font-size:small
}
<font size="3" style="font-size:small

</font><span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small return<span style="font-size:small
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "NA"<font size="3 ;</font><span style="font-size:small
}


View the full article
 
Back
Top