Link to home
Start Free TrialLog in
Avatar of chadmanvb
chadmanvb

asked on

Any way to tell is somone is remoted into a computer using XP with .net 2.0?

I'm working on a script that users would log into a terminal server.  From there they will see test workstations that are available for them to remote desktop to.  I need to know how to tell if there is a remote desktop connection available or if someone else is using it for these test workstations.  Any ideas how I can check for an active remote desktop session running on XP with .net 2.0?  Thanks, Chad
Avatar of mysteriousguy
mysteriousguy

didn't try, but maybe WMI helps you (add a refernce to System.Management)
System.Management.ManagementScope ms = new System.Management.ManagementScope();
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10");
System.Management.ManagementObjectSearcher oSearcher = new System.Management.ManagementObjectSearcher(ms, oq);
System.Management.ManagementObjectCollection oReturnCollection = oSearcher.Get();
 
foreach (System.Management.ManagementObject oReturn in oReturnCollection)
{
    Debug.WriteLine("Name : " + (oReturn["Name"] ?? "").ToString());
    Debug.WriteLine("StartTime: " + (oReturn["StartTime"] ?? "").ToString());
    Debug.WriteLine("Caption: " + (oReturn["Caption"] ?? "").ToString());
    Debug.WriteLine("LogonId: " + (oReturn["LogonId"] ?? "").ToString());
    Debug.WriteLine("LogonType: " + (oReturn["LogonType"] ?? "").ToString());
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mysteriousguy
mysteriousguy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chadmanvb

ASKER

Great!  I will give it a shot tomorrow at work.  Would you happen to know how to change this from c# to vb easily?  Thanks again, Chad
I just tried it, but I guess I am not sure how to get this working in VB.  I imported System.Management.  Any help would be great.  Chad
Was anyone able to change this from C# to vb?  Thanks, Chad
Ok, I have half of it working, but not sure why the other part is not working.  Below is what I have and the first part works great.

  Dim managementScope As New ManagementScope("\\" & TextBox1.Text & "\root\cimv2")
        Dim ojectQuery As New ObjectQuery("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10")
        Dim sessionObjectSearcher As New ManagementObjectSearcher(managementScope, ojectQuery)
        Dim sessionObjectCollection As ManagementObjectCollection = Nothing
        sessionObjectSearcher.Options.Timeout = New System.TimeSpan(0, 0, 5)
        sessionObjectSearcher.Options.ReturnImmediately = True

        sessionObjectCollection = sessionObjectSearcher.Get()


        Dim ManagementObject As ManagementObject
        Dim loginType As String = ""
        Dim strDomain As String = ""

        For Each ManagementObject In sessionObjectCollection

            loginType = ManagementObject("LogonType")
            If loginType = "2" Then
                loginType = "Console"
            Else
                loginType = "Remote Desktop"
            End If

            Exit For
        Next

        MessageBox.Show(loginType)

'*****************everything above here works!!!**********************************


        'create a new query to get the user information for the selected session
        'ObjectQuery associatorQuery = new ObjectQuery(string.Concat("Associators of {Win32_LogonSession.LogonId=", logonId, "} Where AssocClass=Win32_LoggedOnUser Role=Dependent"));
        Dim associatorQuery As New ObjectQuery(String.Concat("Associators of {Win32_LogonSession.LogonId=", logonId, "} Where AssocClass=Win32_LoggedOnUser Role=Dependent"))
'???????????????????????????????????????????????????
        'ObjectQuery(associatorQuery = New ObjectQuery(String.Concat("Associators of {Win32_LogonSession.LogonId=", logonId, "} Where AssocClass=Win32_LoggedOnUser Role=Dependent")))
        'execute the query
        Dim associatorObjectSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(managementScope, associatorQuery)
        Dim associatorObjectCollection As ManagementObjectCollection = Nothing
        associatorObjectCollection = associatorObjectSearcher.Get()

        Dim user As String = ""
        Dim name As String = ""
        Dim domain As String = ""


        'iterate through all users (should only be one result per session)
        For Each ManagementObject In associatorObjectCollection

            'determine properties and output the result
            user = associatorObjectItem("Name")
            name = associatorObjectItem("FullName")
            domain = associatorObjectItem("Domain")
'?????????????????still have errors for these 3 items.

            Exit For

        Next
Works great, I will just try to do this in C#.  Been looking for an excuse to learn it.  Chad