Click here to Skip to main content
13,863,433 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


270 bookmarked
Posted 11 Nov 2009
Licenced CPOL

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

QuestionIt Doesn't show a thing Pin
Member 1415214815-Feb-19 19:45
memberMember 1415214815-Feb-19 19:45 
QuestionLock and Access Pin
Member 1407954212-Dec-18 21:03
memberMember 1407954212-Dec-18 21:03 
Questionbest way for use CredSspSupport and connect to 99% of any RDP Pin
dr.pantagon27-Sep-18 2:10
memberdr.pantagon27-Sep-18 2:10 
QuestionHow to start a service inside the remote system Pin
Arunachalam Gurusamy26-Mar-18 3:14
memberArunachalam Gurusamy26-Mar-18 3:14 
Questionstart the a program when connected Pin
Member 1361793318-Jan-18 5:21
memberMember 1361793318-Jan-18 5:21 
Questionhow can i use that code Pin
Member 1331397617-Jul-17 6:56
memberMember 1331397617-Jul-17 6:56 
Questionserver name , user name , and password fild what comes in it Pin
Member 1331397617-Jul-17 6:54
memberMember 1331397617-Jul-17 6:54 
Questionhow to fill the server, username , and password field plz help me Pin
Member 1331397617-Jul-17 6:52
memberMember 1331397617-Jul-17 6:52 
Questionrdp connection Pin
Kenmod23-Mar-17 12:58
memberKenmod23-Mar-17 12:58 
BugThis does not work for connecting to Azure VMs Pin
Member 1286572726-Nov-16 14:57
memberMember 1286572726-Nov-16 14:57 
Questionwhat is rdp in the code?? Pin
Member 128407049-Nov-16 20:16
memberMember 128407049-Nov-16 20:16 
QuestionCode is good. but how i will get resources like session Printers and session Drivers Pin
Ripdaman Singh12-Oct-16 23:52
memberRipdaman Singh12-Oct-16 23:52 
QuestionWezarp Library for C# Pin
GeekHub25-May-16 0:14
memberGeekHub25-May-16 0:14 
Questionwhat is the class name of rdp object ? Pin
balamurugan210-Feb-16 3:20
memberbalamurugan210-Feb-16 3:20 
AnswerRe: what is the class name of rdp object ? Pin
Perkis5629-Apr-16 9:26
memberPerkis5629-Apr-16 9:26 
AnswerRe: what is the class name of rdp object ? Pin
cyjyau27-Nov-16 20:02
membercyjyau27-Nov-16 20:02 
QuestionMessage Closed Pin
15-Jan-16 1:02
memberAlex Control-F515-Jan-16 1:02 
AnswerRe: Thanks for the great article - Check out my version of this application! Pin
redWingBB22-Mar-16 12:50
memberredWingBB22-Mar-16 12:50 
Questionunable to connect , to window 10 machine from windows 10 machine even no error Pin
Member 115857397-Jan-16 2:21
memberMember 115857397-Jan-16 2:21 
Questionfind problem Pin
Xroute16-Dec-15 3:24
memberXroute16-Dec-15 3:24 
QuestionCode do nothing and no errors!! Pin
Member 1065531626-Oct-15 21:38
memberMember 1065531626-Oct-15 21:38 
AnswerRe: Code do nothing and no errors!! Pin
Member 136020714-Jan-18 6:02
memberMember 136020714-Jan-18 6:02 
Questionwhat to enter in these textboxes u have given in this application Pin
Member 1198496122-Sep-15 0:52
memberMember 1198496122-Sep-15 0:52 
AnswerRe: what to enter in these textboxes u have given in this application Pin
Member 1331397617-Jul-17 6:58
memberMember 1331397617-Jul-17 6:58 
QuestionMFC C++ ? Pin
Member 111658674-Aug-15 14:24
memberMember 111658674-Aug-15 14:24 

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 | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 11 Nov 2009
Article Copyright 2009 by Thiagarajan Alagarsamy
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid