Click here to Skip to main content
14,211,624 members
Click here to Skip to main content
Posted 11 Nov 2009

Tagged as


270 bookmarked

Remote Desktop using C#.NET

Rate this:
4.94 (83 votes)
Please Sign up or sign in to vote.
4.94 (83 votes)
11 Nov 2009     CPOL    
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 -

Comments and Discussions

QuestionRemote desktop Pin
kusum nayal20-Oct-11 2:55
memberkusum nayal20-Oct-11 2:55 
QuestionRemote Desktop accessbility on windows 7 Platform Pin
bveck22-Sep-11 23:13
memberbveck22-Sep-11 23:13 
AnswerRe: Remote Desktop accessbility on windows 7 Platform Pin
Member 837313610-Nov-11 4:11
memberMember 837313610-Nov-11 4:11 
Questioncan i jst restrict user Pin
meeaaoon12-Aug-11 11:47
membermeeaaoon12-Aug-11 11:47 
Questionaccessing file in remote computer through code Pin
prasadkodamana21-Jul-11 0:42
memberprasadkodamana21-Jul-11 0:42 
QuestionCheck out 2X Application Server Client Pin
paulgafa14-Jul-11 22:52
memberpaulgafa14-Jul-11 22:52 
GeneralRemote controle Pin
yacov859416-May-11 6:11
memberyacov859416-May-11 6:11 
GeneralHi. How to connect local resources Pin
Gasanov Tagir23-Apr-11 9:26
memberGasanov Tagir23-Apr-11 9:26 
GeneralRe: Hi. How to connect local resources Pin
tomcode14-Jun-11 20:10
membertomcode14-Jun-11 20:10 
GeneralRe: Hi. How to connect local resources Pin
Gasanov Tagir18-Jun-11 2:40
memberGasanov Tagir18-Jun-11 2:40 
GeneralRe: Hi. How to connect local resources Pin
mayakingka3-Apr-12 18:43
membermayakingka3-Apr-12 18:43 
GeneralMy vote of 3 Pin
qinghan198918-Apr-11 21:57
memberqinghan198918-Apr-11 21:57 
QuestionResolution and colors Pin
Jackman7116-Apr-11 20:53
memberJackman7116-Apr-11 20:53 
GeneralNo output Pin
Ananya hs14-Mar-11 23:49
memberAnanya hs14-Mar-11 23:49 
GeneralMy vote of 5 Pin
jintao123413-Mar-11 16:34
memberjintao123413-Mar-11 16:34 
QuestionNeed to know how to logoff not just disconnect... Pin
Sukar10-Dec-10 8:19
memberSukar10-Dec-10 8:19 
QuestionHow to connect to remote desktop from console application? Pin
Tesic Goran10-Nov-10 2:40
professionalTesic Goran10-Nov-10 2:40 
GeneralMy vote of 2 Pin
Dave Kreskowiak25-Sep-10 6:02
mveDave Kreskowiak25-Sep-10 6:02 
Generalwithout logging out the user Pin
vdfoo24-Aug-10 22:13
membervdfoo24-Aug-10 22:13 
GeneralRe: without logging out the user Pin
Dave Kreskowiak25-Sep-10 6:00
mveDave Kreskowiak25-Sep-10 6:00 
GeneralMy vote of 5 Pin
javiava5-Aug-10 23:29
memberjaviava5-Aug-10 23:29 
GeneralRDP server configuration Pin
Nanung1-Jul-10 15:53
memberNanung1-Jul-10 15:53 
GeneralGreat app...quick question about OnAuthenticationWarningDisplayed Event Pin
carlf22-Jun-10 8:27
membercarlf22-Jun-10 8:27 
QuestionMSTSCLib.dll and AxMSTSCLib - exist on every windows? Pin
elad21092-Jun-10 18:49
memberelad21092-Jun-10 18:49 
GeneralRemote desktop application in .net 2008 Pin
Member 439593713-May-10 23:03
memberMember 439593713-May-10 23:03 

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.

Layout: fixed | fluid

Article Copyright 2009 by Thiagarajan Alagarsamy
Everything else Copyright © CodeProject, 1999-2019

Server Web01
Version 2.8.190619.2