Click here to Skip to main content
13,295,441 members (35,140 online)
Click here to Skip to main content
Add your own
alternative version


264 bookmarked
Posted 11 Nov 2009

Remote Desktop using C#.NET

, 11 Nov 2009
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

Questionremote desktop Pin
jelani dejonge7-Dec-13 7:30
memberjelani dejonge7-Dec-13 7:30 
QuestionRemote desktop Pin
kuldeep singh201221-Oct-13 4:02
groupkuldeep singh201221-Oct-13 4:02 
QuestionCompatible with Windows 8? Pin
Member 91121273-Oct-13 21:47
memberMember 91121273-Oct-13 21:47 
AnswerRe: Compatible with Windows 8? Pin
Justin Nichsolson11-Mar-14 20:44
memberJustin Nichsolson11-Mar-14 20:44 
GeneralRe: Compatible with Windows 8? Pin
Member 1331397620-Jul-17 20:51
memberMember 1331397620-Jul-17 20:51 
Questioncan we writing such app with mvc? if yes, can you share the code Pin
Member 84904062-Sep-13 0:30
memberMember 84904062-Sep-13 0:30 
GeneralMy vote of 5 Pin
wsc09189-Jul-13 17:59
memberwsc09189-Jul-13 17:59 
GeneralMy vote of 5 Pin
manoj kumar choubey4-Jul-13 3:17
membermanoj kumar choubey4-Jul-13 3:17 
QuestionRemote Desktop without GUI Pin
nirali133-Jun-13 21:39
membernirali133-Jun-13 21:39 
QuestionUrgent. Pin
Member 919961929-Apr-13 10:40
memberMember 919961929-Apr-13 10:40 
AnswerRe: Urgent. Pin
Christopher Smit17-Oct-13 7:32
memberChristopher Smit17-Oct-13 7:32 
Question? Pin
WHITELIONX26-Apr-13 20:57
memberWHITELIONX26-Apr-13 20:57 
AnswerRe: ? Pin
Justin Nichsolson11-Mar-14 20:42
memberJustin Nichsolson11-Mar-14 20:42 
QuestionPort number Pin
yunuskose22-Apr-13 2:22
memberyunuskose22-Apr-13 2:22 
Not port number in Terminal services controls properties
AnswerRe: Port number Pin
Member 794829922-Aug-13 21:34
memberMember 794829922-Aug-13 21:34 
QuestionRDP for console Pin
source.compiler20-Apr-13 10:15
membersource.compiler20-Apr-13 10:15 
QuestionUnable to connect to wireless laptop which is in same network connected to wireless Pin
Sivanandan16-Apr-13 6:18
memberSivanandan16-Apr-13 6:18 
GeneralMy vote of 5 Pin
Member 83360121-Mar-13 0:28
memberMember 83360121-Mar-13 0:28 
BugCant connect using PORT Pin
venkatmca00813-Feb-13 22:15
membervenkatmca00813-Feb-13 22:15 
GeneralRe: Cant connect using PORT Pin
Armin Rezayati29-Apr-14 23:56
memberArmin Rezayati29-Apr-14 23:56 
BugRemote screen is not displayed Pin
thakurajay9330-Jan-13 19:36
memberthakurajay9330-Jan-13 19:36 
Bugbut it is not showing the desktop screen after click on connect Pin
bhupendra8816-Dec-12 6:54
memberbhupendra8816-Dec-12 6:54 
QuestionHow can I logout and NOT Disconnect? Pin
Member 817647311-Nov-12 22:20
memberMember 817647311-Nov-12 22:20 
QuestionI can able to connect to other system but the remote system is logging off is there any solution for this. Pin
tejas.chudasama2-Nov-12 9:38
membertejas.chudasama2-Nov-12 9:38 
AnswerRe: I can able to connect to other system but the remote system is logging off is there any solution for this. Pin
Adrian Viegas7-Nov-12 23:27
memberAdrian Viegas7-Nov-12 23:27 
Questionc# remote desktop managing application Pin
Member 328966114-Sep-12 5:04
memberMember 328966114-Sep-12 5:04 
Questionremote desktoping Pin
riti kapoor14-Aug-12 6:52
memberriti kapoor14-Aug-12 6:52 
GeneralMy vote of 5 Pin
Farhan Ghumra6-Aug-12 22:50
memberFarhan Ghumra6-Aug-12 22:50 
GeneralMy vote of 5 Pin
Kanasz Robert6-Aug-12 22:48
mvpKanasz Robert6-Aug-12 22:48 
QuestionHow i can Remote a computer web connection Pin
jprogramer23-Jun-12 23:50
memberjprogramer23-Jun-12 23:50 
QuestionRemote system is logging off Pin
darvisunny10-Apr-12 19:32
memberdarvisunny10-Apr-12 19:32 
QuestionNot working over internet? Pin
Member 875666924-Mar-12 2:16
memberMember 875666924-Mar-12 2:16 
Questionhello, how do I connect my own computer? Pin
jeferson Medeiros14-Mar-12 3:57
memberjeferson Medeiros14-Mar-12 3:57 
QuestionHow to start calc.exe on the remote computer? Pin
jkoorts25-Jan-12 23:05
memberjkoorts25-Jan-12 23:05 
QuestionSo where's the tabs? Pin
Bill Doerfler5-Jan-12 4:12
memberBill Doerfler5-Jan-12 4:12 
AnswerRe: So where's the tabs? Pin
Member 806538828-Feb-12 7:26
memberMember 806538828-Feb-12 7:26 
GeneralMy vote of 1 Pin
Member 850269719-Dec-11 20:56
memberMember 850269719-Dec-11 20:56 
QuestionResize Remote Desktop Connection? Pin
jlopez78810-Nov-11 6:19
memberjlopez78810-Nov-11 6:19 
AnswerRe: Resize Remote Desktop Connection? Pin
Matt Webb5-Jan-12 5:45
memberMatt Webb5-Jan-12 5:45 
QuestionRemote desktop Pin
kusum nayal20-Oct-11 3:55
memberkusum nayal20-Oct-11 3:55 
QuestionRemote Desktop accessbility on windows 7 Platform Pin
bveck23-Sep-11 0:13
memberbveck23-Sep-11 0:13 
AnswerRe: Remote Desktop accessbility on windows 7 Platform Pin
Member 837313610-Nov-11 5:11
memberMember 837313610-Nov-11 5:11 
Questioncan i jst restrict user Pin
meeaaoon12-Aug-11 12:47
membermeeaaoon12-Aug-11 12:47 
Questionaccessing file in remote computer through code Pin
prasadkodamana21-Jul-11 1:42
memberprasadkodamana21-Jul-11 1:42 
QuestionCheck out 2X Application Server Client Pin
paulgafa14-Jul-11 23:52
memberpaulgafa14-Jul-11 23:52 
GeneralRemote controle Pin
yacov859416-May-11 7:11
memberyacov859416-May-11 7:11 
GeneralHi. How to connect local resources Pin
Gasanov23-Apr-11 10:26
memberGasanov23-Apr-11 10:26 
GeneralRe: Hi. How to connect local resources Pin
tomcode14-Jun-11 21:10
membertomcode14-Jun-11 21:10 
GeneralRe: Hi. How to connect local resources Pin
Gasanov18-Jun-11 3:40
memberGasanov18-Jun-11 3:40 
GeneralRe: Hi. How to connect local resources Pin
mayakingka3-Apr-12 19:43
membermayakingka3-Apr-12 19:43 

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.

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