Solved

Using C# to grab RDP session information

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

832 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