Solved

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

Posted on 2009-07-07
7
459 Views
Last Modified: 2013-11-07
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
0
Comment
Question by:chadmanvb
  • 5
  • 2
7 Comments
 
LVL 4

Expert Comment

by:mysteriousguy
ID: 24800515
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

0
 
LVL 4

Accepted Solution

by:
mysteriousguy earned 500 total points
ID: 24801409
I just tried my solution. The complete code is attached
//create a local management scope

ManagementScope managementScope = new ManagementScope();

//create a query for all available sessions (LogonType 2 = Interactive; LogonType 10 = RemoteInteractive)

ObjectQuery sessionQuery = new ObjectQuery("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10");

// execute the query

ManagementObjectSearcher sessionObjectSearcher = new ManagementObjectSearcher(managementScope, sessionQuery);

ManagementObjectCollection sessionObjectCollection = sessionObjectSearcher.Get();
 

//iterate through all sessions

foreach (ManagementObject sessionObjectItem in sessionObjectCollection)

{

    //determine properties

    uint logonType = (uint)sessionObjectItem["LogonType"];

    string logonId = sessionObjectItem["LogonId"] as string ;
 

    if (logonType == 2)

    {

        Debug.WriteLine("Logon Type: Console");

    }

    else

    {

        Debug.WriteLine("Logon Type: RDP/Terminal Server");

    }
 

    //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"));

    // execute the query

    ManagementObjectSearcher associatorObjectSearcher = new ManagementObjectSearcher(managementScope, associatorQuery);

    ManagementObjectCollection associatorObjectCollection = associatorObjectSearcher.Get();
 

    //iterate through all users (should only be one result per session)

    foreach (ManagementObject associatorObjectItem in associatorObjectCollection)

    {

        //determine properties and output the result

        string user = associatorObjectItem["Name"] as string;

        string name = associatorObjectItem["FullName"] as string;

        string domain = associatorObjectItem["Domain"] as string;

        Debug.WriteLine(string.Concat("User: ", user));

        Debug.WriteLine(string.Concat("FullName: " , name));

        Debug.WriteLine(string.Concat("Domain: ", domain));

    }
 

    Debug.WriteLine(string.Concat("Session start time: ", sessionObjectItem["StartTime"]));

}

Open in new window

0
 

Author Comment

by:chadmanvb
ID: 24804847
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
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:chadmanvb
ID: 24814387
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
0
 

Author Comment

by:chadmanvb
ID: 24823580
Was anyone able to change this from C# to vb?  Thanks, Chad
0
 

Author Comment

by:chadmanvb
ID: 24826298
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
0
 

Author Closing Comment

by:chadmanvb
ID: 31600858
Works great, I will just try to do this in C#.  Been looking for an excuse to learn it.  Chad
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 47
asp.net mvc input file 2 32
How to add <appSettings> to the webconfig file 2 20
Get Client IP on RDS - VB.NET 15 20
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now