Solved

Using C# to grab RDP session information

Posted on 2011-09-22
2
3,042 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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

15 Experts available now in Live!

Get 1:1 Help Now