Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using C# to grab RDP session information

Posted on 2011-09-22
2
Medium Priority
?
3,923 Views
Last Modified: 2013-12-17
I've been trying to figure out how to get RDP session information with C#. There's a program that I'm writing, and I want some functionality to differ based on whether or not the user is logged in through RDP. I know how to check to see if the user is on an RDP session, using System.Windows.Forms.SystemInformation, but aside from that I'm not sure how to retrieve the client's machine name, the domain controller that processed the login, and

I don't want to rely on environment variables, and I really don't want to include a third-party library to do it. Any suggestions? Thanks!
0
Comment
Question by:elorc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Assisted Solution

by:dj_alik
dj_alik earned 1000 total points
ID: 36585367
Grabbing Information of a Terminal Services Session Programmatically
http://www.codeproject.com/Articles/111430/Grabbing-Information-of-a-Terminal-Services-Sessio
0
 
LVL 38

Accepted Solution

by:
Rich Rumble earned 1000 total points
ID: 36586101
Try this, its basically using wmi to get the info
-rich
using System;
using System.Linq;
using System.Management;

namespace WMIGen
{
	public class Win32_TerminalService_Query
	{
		public static int Main(string[] args)
		{
			try
			{
				string computer = string.Empty;
				if (args.Count() == 1)
				{
					computer = @"\\" + args[0] + @"\";
				}

				ManagementObjectSearcher searcher = new ManagementObjectSearcher(computer + @"root\CIMV2", "SELECT * FROM Win32_TerminalService");
				ManagementObjectCollection colItems = searcher.Get( );

				Console.WriteLine( "{0} instance{1}", colItems.Count, ( colItems.Count == 1 ? String.Empty : "s" ) );
				Console.WriteLine( );

				foreach ( ManagementObject queryObj in colItems )
				{
					Console.WriteLine("AcceptPause             : {0}", queryObj["AcceptPause"]);
					Console.WriteLine("AcceptStop              : {0}", queryObj["AcceptStop"]);
					Console.WriteLine("Caption                 : {0}", queryObj["Caption"]);
					Console.WriteLine("CheckPoint              : {0}", queryObj["CheckPoint"]);
					Console.WriteLine("CreationClassName       : {0}", queryObj["CreationClassName"]);
					Console.WriteLine("Description             : {0}", queryObj["Description"]);
					Console.WriteLine("DesktopInteract         : {0}", queryObj["DesktopInteract"]);
					Console.WriteLine("DisconnectedSessions    : {0}", queryObj["DisconnectedSessions"]);
					Console.WriteLine("DisplayName             : {0}", queryObj["DisplayName"]);
					Console.WriteLine("ErrorControl            : {0}", queryObj["ErrorControl"]);
					Console.WriteLine("ExitCode                : {0}", queryObj["ExitCode"]);
					Console.WriteLine("InstallDate             : {0}", queryObj["InstallDate"]);
					Console.WriteLine("Name                    : {0}", queryObj["Name"]);
					Console.WriteLine("PathName                : {0}", queryObj["PathName"]);
					Console.WriteLine("ProcessId               : {0}", queryObj["ProcessId"]);
					Console.WriteLine("ServiceSpecificExitCode : {0}", queryObj["ServiceSpecificExitCode"]);
					Console.WriteLine("ServiceType             : {0}", queryObj["ServiceType"]);
					Console.WriteLine("Started                 : {0}", queryObj["Started"]);
					Console.WriteLine("StartMode               : {0}", queryObj["StartMode"]);
					Console.WriteLine("StartName               : {0}", queryObj["StartName"]);
					Console.WriteLine("State                   : {0}", queryObj["State"]);
					Console.WriteLine("Status                  : {0}", queryObj["Status"]);
					Console.WriteLine("SystemCreationClassName : {0}", queryObj["SystemCreationClassName"]);
					Console.WriteLine("SystemName              : {0}", queryObj["SystemName"]);
					Console.WriteLine("TagId                   : {0}", queryObj["TagId"]);
					Console.WriteLine("TotalSessions           : {0}", queryObj["TotalSessions"]);
					Console.WriteLine("WaitHint                : {0}", queryObj["WaitHint"]);
					Console.WriteLine();
				}
				return 0;
			}
			catch (Exception e)
			{
				Console.Error.WriteLine("An error occurred while querying WMI: {0}", e.Message);
				return 1;
			}
		}
	}
}

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

670 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