Click here to Skip to main content
12,289,383 members (73,483 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


255 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

Questionwhat is the class name of rdp object ? Pin
balamurugan210-Feb-16 2:20
memberbalamurugan210-Feb-16 2:20 
AnswerRe: what is the class name of rdp object ? Pin
Perkis5629-Apr-16 8:26
memberPerkis5629-Apr-16 8:26 
QuestionThanks for the great article - Check out my version of this application! Pin
Alex Control-F515-Jan-16 0:02
memberAlex Control-F515-Jan-16 0:02 
AnswerRe: Thanks for the great article - Check out my version of this application! Pin
redWingBB22-Mar-16 11:50
memberredWingBB22-Mar-16 11:50 
Questionunable to connect , to window 10 machine from windows 10 machine even no error Pin
Member 115857397-Jan-16 1:21
memberMember 115857397-Jan-16 1:21 
Questionfind problem Pin
Xroute16-Dec-15 2:24
memberXroute16-Dec-15 2:24 
QuestionCode do nothing and no errors!! Pin
Member 1065531626-Oct-15 20:38
memberMember 1065531626-Oct-15 20:38 
Questionwhat to enter in these textboxes u have given in this application Pin
Member 1198496121-Sep-15 23:52
memberMember 1198496121-Sep-15 23:52 
QuestionMFC C++ ? Pin
Member 111658674-Aug-15 13:24
memberMember 111658674-Aug-15 13:24 
QuestionLog off Pin
Sohil.sminfoware1-Jun-15 19:57
memberSohil.sminfoware1-Jun-15 19:57 
AnswerRe: Log off Pin
Member 1109730924-Apr-16 12:03
memberMember 1109730924-Apr-16 12:03 
QuestionOn Win8.1 box i am not able to connect Pin
sayedfar18-May-15 23:43
membersayedfar18-May-15 23:43 
QuestionCode to keep user session active in the server Pin
Prashant Bangaluru12-May-15 18:14
memberPrashant Bangaluru12-May-15 18:14 
Questionrdp without gui Pin
libin04106-May-15 15:56
memberlibin04106-May-15 15:56 
QuestionMicrosoft Terminal services Control type librery Pin
Member 116556361-May-15 12:07
memberMember 116556361-May-15 12:07 
AnswerRe: Microsoft Terminal services Control type librery Pin
Garth J Lancaster1-May-15 13:49
memberGarth J Lancaster1-May-15 13:49 
GeneralRe: Microsoft Terminal services Control type librery Pin
Member 116556361-May-15 14:08
memberMember 116556361-May-15 14:08 
GeneralRe: Microsoft Terminal services Control type librery Pin
Garth J Lancaster3-May-15 11:48
memberGarth J Lancaster3-May-15 11:48 
QuestionServer Name can be a remote IP? Pin
almendietamor19-Feb-15 11:54
memberalmendietamor19-Feb-15 11:54 
AnswerRe: Server Name can be a remote IP? Pin
Member 116556361-May-15 13:36
memberMember 116556361-May-15 13:36 
GeneralRe: Server Name can be a remote IP? Pin
almendietamor1-May-15 18:47
memberalmendietamor1-May-15 18:47 
Questionthe code doesn work Pin
Member 1133148628-Dec-14 21:31
memberMember 1133148628-Dec-14 21:31 
AnswerRe: the code doesn work Pin
Garth J Lancaster28-Dec-14 21:42
memberGarth J Lancaster28-Dec-14 21:42 
GeneralRe: the code doesn work Pin
Member 1133148629-Dec-14 21:42
memberMember 1133148629-Dec-14 21:42 
GeneralRe: the code doesn work Pin
Garth J Lancaster29-Dec-14 23:17
memberGarth J Lancaster29-Dec-14 23:17 
AnswerRe: the code doesn work Pin
Member 116556361-May-15 13:38
memberMember 116556361-May-15 13:38 
AnswerRe: the code doesn work Pin
Member 409349728-Sep-15 0:51
memberMember 409349728-Sep-15 0:51 
QuestionImpressoras Locais Pin
Member 806117212-Nov-14 3:32
memberMember 806117212-Nov-14 3:32 
QuestionNot able to connect to win server2008 r2 from local win 7 Pin
Debasis Chattopadhyay21-Oct-14 3:22
memberDebasis Chattopadhyay21-Oct-14 3:22 
QuestionDynamically adding tabs Pin
stephenpe12311-Sep-14 19:55
memberstephenpe12311-Sep-14 19:55 
QuestionNO Desktop View Pin
Pkcse6-Sep-14 3:17
memberPkcse6-Sep-14 3:17 
AnswerRe: NO Desktop View Pin
Member 116556361-May-15 13:40
memberMember 116556361-May-15 13:40 
QuestionAudio Recorder Remote Pin
MatBot8829-Aug-14 0:51
memberMatBot8829-Aug-14 0:51 
QuestionIsolated environment running in a sandbox type Pin
jungyeol park17-Aug-14 16:11
memberjungyeol park17-Aug-14 16:11 
QuestionConfiguration Protocol RDP - MSTC with medium trust Pin
Member 100225477-Aug-14 2:18
memberMember 100225477-Aug-14 2:18 
BugUnable to get the window handle for the 'AxMsTscAxNotSafeForScripting' control problem Pin
Member 1086155111-Jun-14 23:42
memberMember 1086155111-Jun-14 23:42 
Questionconnection Pin
antonio_l19-May-14 23:15
memberantonio_l19-May-14 23:15 
Questioninvoke RDP client from a web application (ASP.NET) Pin
Ramkumar Vasudevan8-Apr-14 23:21
memberRamkumar Vasudevan8-Apr-14 23:21 
QuestionRDP Successful Login Check Pin
Justin Nichsolson11-Mar-14 19:39
memberJustin Nichsolson11-Mar-14 19:39 
QuestionRemote desktop only with shell? Pin
chathu03j26-Feb-14 19:28
memberchathu03j26-Feb-14 19:28 
Questionthank you! could this program support win 8? Pin
ramip2-Feb-14 21:45
memberramip2-Feb-14 21:45 
AnswerRe: thank you! could this program support win 8? Pin
Justin Nichsolson11-Mar-14 19:43
memberJustin Nichsolson11-Mar-14 19:43 
AnswerRe: thank you! could this program support win 8? Pin
boobssMan19-Mar-14 0:45
memberboobssMan19-Mar-14 0:45 
Questionremote desktop Pin
jelani dejonge7-Dec-13 6:30
memberjelani dejonge7-Dec-13 6:30 
QuestionRemote desktop Pin
kuldeep singh201221-Oct-13 3:02
groupkuldeep singh201221-Oct-13 3:02 
QuestionCompatible with Windows 8? Pin
Member 91121273-Oct-13 20:47
memberMember 91121273-Oct-13 20:47 
AnswerRe: Compatible with Windows 8? Pin
Justin Nichsolson11-Mar-14 19:44
memberJustin Nichsolson11-Mar-14 19:44 
Questioncan we writing such app with mvc? if yes, can you share the code Pin
Member 84904061-Sep-13 23:30
memberMember 84904061-Sep-13 23:30 
GeneralMy vote of 5 Pin
wsc09189-Jul-13 16:59
memberwsc09189-Jul-13 16:59 
GeneralMy vote of 5 Pin
manoj kumar choubey4-Jul-13 2:17
membermanoj kumar choubey4-Jul-13 2:17 

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
Web01 | 2.8.160518.1 | Last Updated 11 Nov 2009
Article Copyright 2009 by Thiagarajan Alagarsamy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid