Click here to Skip to main content
11,929,481 members (53,131 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


252 bookmarked

Remote Desktop using C#.NET

, 11 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
This article is about showing how to create a .NET application to perform remote desktop operation using Microsoft Terminal Services Client ActiveX control.


Remote Desktop Services is one of Microsoft Windows components to access a remote computer through the network. Only the user interface of the application is presented at the client. Any input is redirected over to the remote computer over the network.

At work, we use Remote Desktop a great deal. It allows us to login to a remote server to perform health checks, deploy applications, troubleshoot problems, etc. We also use remote desktop often when we do WFH (work from home).

Why do you want to write a .NET application to do this when you have the MS Terminal Services client available from OS? Well, consider if you want to work on 3 different application servers at the same time and want to toggle between these 3 servers quite often. With the MSTSC, we will be running 3 different clients for the 3 servers and it is difficult to manage the working environment. In .NET, you can develop an application with tab control to load remote desktop sessions in different tabs in one window.


We will be using AxMSTSCLib an ActiveX component in our program to connect to the remote computer. It’s not that hard to build a remote desktop application in .NET. Microsoft has a “Microsoft RDP client control” ActiveX control that we will be using in our application.

This is How We Do It

We will start by creating a Windows application in the Visual Studio IDE.

Add a reference to “Microsoft Terminal Services Control Type Library” from the COM tab. This will add MSTSCLib.dll to the project.

Sample Image - maximum width is 600 pixels

To add MSTSC to the toolbox, right click the toolbox and select “Choose Items…”. Now add “Microsoft Terminal Services control from the COM tab.

Sample Image - maximum width is 600 pixels

Drag the newly added control from toolbox to the form.

Add 3 textbox and 2 button controls to the form:

Sample Image - maximum width is 600 pixels

Connect Button - Click Event

Here is how we write the Connect button click event.

rdp.Server = txtServer.Text;
rdp.UserName = txtUserName.Text;
IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = txtPassword.Text;

Now assign the properties (Server, UserName) of RDP control with the textbox values.

Here’s how easy it is to login to remote machine. However there is one catch, there is no direct method in RDP control through which you can pass the username and password to login to the remote desktop.

Due to security reasons, you have to implement an interface (IMsTscNonScriptable) to cast it separately.

IMsTscNonScriptable secured = IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = txtPassword.Text; 

Disconnect Button – Click Event

To disconnect from the remote desktop session, we just need to call the Disconnect() method.

Before disconnecting, we want to ensure that the connection is still available. We don't want to disconnect if it is already disconnected (very clever, huh).

if (rdp.Connected.ToString() == "1")

That’s all folks!


  • 5th November, 2009: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Thiagarajan Alagarsamy
Web Developer
India India
Thiagu is living in Bangalore, India. He has started coding when he was 12 years old. His native is Madurai, a historic city in south India. He loves to code in C#. He frequents code project when he is not coding. Thiagu loves reading Dan Brown and Michael Crichton novels. He is very much interested in Artificial Intelligence (AI). To view his blog -

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
jintao123413-Mar-11 17:34
memberjintao123413-Mar-11 17:34 
QuestionNeed to know how to logoff not just disconnect... Pin
Sukar10-Dec-10 9:19
memberSukar10-Dec-10 9:19 
QuestionHow to connect to remote desktop from console application? Pin
Tesic Goran10-Nov-10 3:40
memberTesic Goran10-Nov-10 3:40 
GeneralMy vote of 2 Pin
Dave Kreskowiak25-Sep-10 7:02
mvpDave Kreskowiak25-Sep-10 7:02 
Generalwithout logging out the user Pin
vdfoo24-Aug-10 23:13
membervdfoo24-Aug-10 23:13 
GeneralRe: without logging out the user Pin
Dave Kreskowiak25-Sep-10 7:00
mvpDave Kreskowiak25-Sep-10 7:00 
GeneralMy vote of 5 Pin
javiava6-Aug-10 0:29
memberjaviava6-Aug-10 0:29 
GeneralRDP server configuration Pin
Nanung1-Jul-10 16:53
memberNanung1-Jul-10 16:53 
GeneralGreat app...quick question about OnAuthenticationWarningDisplayed Event Pin
carlf22-Jun-10 9:27
membercarlf22-Jun-10 9:27 
QuestionMSTSCLib.dll and AxMSTSCLib - exist on every windows? Pin
elad21092-Jun-10 19:49
memberelad21092-Jun-10 19:49 
GeneralRemote desktop application in .net 2008 Pin
Member 439593714-May-10 0:03
memberMember 439593714-May-10 0:03 
GeneralOther port than 3389 Pin
johngreene12-May-10 6:13
memberjohngreene12-May-10 6:13 
GeneralRe: Other port than 3389 Pin
johngreene12-May-10 6:26
memberjohngreene12-May-10 6:26 
GeneralAutomated logoff via rdp (not jsut disconnection) Pin
elad21096-May-10 6:41
memberelad21096-May-10 6:41 

I'm using a rdp control like the one here

I have connected a local user.

I want that the control will logoff the user (disconnecting the user isn't enough) via c# code after some time-out period.

I'm doing all that because apparently the log on scripts works only after a proper logoff and not just disconnection/exit.

Does someone know how is it done?
QuestionHow to Use this in internet Pin
syam@tnt25-Apr-10 8:54
membersyam@tnt25-Apr-10 8:54 
AnswerRe: How to Use this in internet Pin
Thiagarajan Alagarsamy6-May-10 19:00
memberThiagarajan Alagarsamy6-May-10 19:00 
Generalcreate new AxMSTSCLib [modified] Pin
elad210914-Apr-10 4:56
memberelad210914-Apr-10 4:56 
GeneralCompile error in Visual Studio 10 (2010) beta 2: AxMsTscAxNotSafeForScripting [modified] Pin
Dr.Lightning201019-Mar-10 15:23
memberDr.Lightning201019-Mar-10 15:23 
GeneralRe: Compile error in Visual Studio 10 (2010) beta 2: AxMsTscAxNotSafeForScripting Pin
66977527-May-10 18:14
member66977527-May-10 18:14 
QuestionProblem in RDP Pin
naim khan4-Mar-10 21:28
membernaim khan4-Mar-10 21:28 
QuestionPocket PC Pin
beespace17-Feb-10 0:26
memberbeespace17-Feb-10 0:26 
GeneralHi Thiagu Pin
Nezam Ahamed14-Feb-10 5:17
memberNezam Ahamed14-Feb-10 5:17 
GeneralThanks but why not using AdvancedSettings2 Pin
Kam9-Jan-10 12:17
memberKam9-Jan-10 12:17 
GeneralHi! Nice article Pin
Virat Kothari17-Nov-09 9:08
memberVirat Kothari17-Nov-09 9:08 
GeneralVery nice -- thanks! Pin
Bit-Smacker17-Nov-09 8:21
memberBit-Smacker17-Nov-09 8:21 
GeneralTried this is Access 2007 VBA Pin
dpminusa16-Nov-09 19:02
memberdpminusa16-Nov-09 19:02 
GeneralI've done this before myself, and there's RDTabs if you want it Pin
Michael Adams16-Nov-09 16:57
memberMichael Adams16-Nov-09 16:57 
QuestionThis looks very similar to an earlier article. Pin
dpminusa16-Nov-09 16:27
memberdpminusa16-Nov-09 16:27 
QuestionWhat about Terminals? Pin
Garth Arnold16-Nov-09 10:29
memberGarth Arnold16-Nov-09 10:29 
GeneralQuick and to the point! Pin
TomB16-Nov-09 10:23
memberTomB16-Nov-09 10:23 
QuestionWhat about mRemote? Pin
Member 178098912-Nov-09 9:00
memberMember 178098912-Nov-09 9:00 
AnswerRe: What about mRemote? Pin
Thiagarajan Alagarsamy12-Nov-09 20:35
memberThiagarajan Alagarsamy12-Nov-09 20:35 
Generalsimple.. Pin
sashidhar12-Nov-09 6:07
membersashidhar12-Nov-09 6:07 
GeneralRe: simple.. Pin
Thiagarajan Alagarsamy12-Nov-09 20:34
memberThiagarajan Alagarsamy12-Nov-09 20:34 
GeneralMy vote of 2 Pin
Saravanan Chinnusamy11-Nov-09 20:24
memberSaravanan Chinnusamy11-Nov-09 20:24 
GeneralRe: My vote of 2 Pin
Thiagarajan Alagarsamy12-Nov-09 0:10
memberThiagarajan Alagarsamy12-Nov-09 0:10 
AnswerRe: My vote of 2 Pin
Bit-Smacker17-Nov-09 8:18
memberBit-Smacker17-Nov-09 8:18 
GeneralHI Pin
Ravenet11-Nov-09 16:31
memberRavenet11-Nov-09 16:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 11 Nov 2009
Article Copyright 2009 by Thiagarajan Alagarsamy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid