Solved

Using C# to grab RDP session information

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert VB web project to C# 3 67
AJAX Create / Update Not working MVC 5 67
aspx ascx, c# 7 45
Manage big list of parameter list 8 26
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…
Learn about cloud computing and its benefits for small business owners.

752 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