Solved

Using C# to grab RDP session information

Posted on 2011-09-22
2
3,355 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
2 Comments
 
LVL 18

Assisted Solution

by:dj_alik
dj_alik earned 250 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 250 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Function after success of Jquery/AJAX action 9 29
Add a Condition in ASP Repeater 6 34
how to just get time from a date 6 34
Regarding swagger API 1 17
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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