Click here to Skip to main content
Click here to Skip to main content

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 -

Comments and Discussions

QuestionServer Name can be a remote IP? Pinmemberalmendietamor19-Feb-15 12:54 
Questionthe code doesn work PinmemberMember 1133148628-Dec-14 22:31 
AnswerRe: the code doesn work PinmemberGarth J Lancaster28-Dec-14 22:42 
GeneralRe: the code doesn work PinmemberMember 1133148629-Dec-14 22:42 
GeneralRe: the code doesn work PinmemberGarth J Lancaster30-Dec-14 0:17 
QuestionImpressoras Locais PinmemberMember 806117212-Nov-14 4:32 
QuestionNot able to connect to win server2008 r2 from local win 7 PinmemberDebasis Chattopadhyay21-Oct-14 4:22 
QuestionDynamically adding tabs Pinmemberstephenpe12311-Sep-14 20:55 
QuestionNO Desktop View PinmemberPkcse6-Sep-14 4:17 
QuestionAudio Recorder Remote PinmemberMatBot8829-Aug-14 1:51 
QuestionIsolated environment running in a sandbox type Pinmemberjungyeol park17-Aug-14 17:11 
QuestionConfiguration Protocol RDP - MSTC with medium trust PinmemberMember 100225477-Aug-14 3:18 
BugUnable to get the window handle for the 'AxMsTscAxNotSafeForScripting' control problem PinmemberMember 1086155112-Jun-14 0:42 
Questionconnection Pinmemberantonio_l20-May-14 0:15 
Questioninvoke RDP client from a web application (ASP.NET) PinmemberRamkumar Vasudevan9-Apr-14 0:21 
QuestionRDP Successful Login Check PinmemberJustin Nichsolson11-Mar-14 20:39 
QuestionRemote desktop only with shell? Pinmemberchathu03j26-Feb-14 20:28 
Questionthank you! could this program support win 8? Pinmemberramip2-Feb-14 22:45 
AnswerRe: thank you! could this program support win 8? PinmemberJustin Nichsolson11-Mar-14 20:43 
AnswerRe: thank you! could this program support win 8? PinmemberboobssMan19-Mar-14 1:45 
Questionremote desktop Pinmemberjelani dejonge7-Dec-13 7:30 
QuestionRemote desktop Pingroupkuldeep singh201221-Oct-13 4:02 
QuestionCompatible with Windows 8? PinmemberMember 91121273-Oct-13 21:47 
AnswerRe: Compatible with Windows 8? PinmemberJustin Nichsolson11-Mar-14 20:44 
Questioncan we writing such app with mvc? if yes, can you share the code PinmemberMember 84904062-Sep-13 0:30 
GeneralMy vote of 5 Pinmemberwsc09189-Jul-13 17:59 
GeneralMy vote of 5 Pinmembermanoj kumar choubey4-Jul-13 3:17 
QuestionRemote Desktop without GUI Pinmembernirali133-Jun-13 21:39 
QuestionUrgent. PinmemberMember 919961929-Apr-13 10:40 
AnswerRe: Urgent. PinmemberChristopher Smit17-Oct-13 7:32 
Question? PinmemberWHITELIONX26-Apr-13 20:57 
AnswerRe: ? PinmemberJustin Nichsolson11-Mar-14 20:42 
QuestionPort number Pinmemberyunuskose22-Apr-13 2:22 
AnswerRe: Port number PinmemberMember 794829922-Aug-13 21:34 
QuestionRDP for console Pinmembersource.compiler20-Apr-13 10:15 
QuestionUnable to connect to wireless laptop which is in same network connected to wireless PinmemberSivanandan16-Apr-13 6:18 
GeneralMy vote of 5 PinmemberMember 83360121-Mar-13 0:28 
BugCant connect using PORT Pinmembervenkatmca00813-Feb-13 22:15 
GeneralRe: Cant connect using PORT PinmemberArmin Rezayati29-Apr-14 23:56 
BugRemote screen is not displayed Pinmemberthakurajay9330-Jan-13 19:36 
Bugbut it is not showing the desktop screen after click on connect Pinmemberbhupendra8816-Dec-12 6:54 
QuestionHow can I logout and NOT Disconnect? PinmemberMember 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. Pinmembertejas.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. PinmemberAdrian Viegas7-Nov-12 23:27 
Questionc# remote desktop managing application PinmemberMember 328966114-Sep-12 5:04 
2 users are connected through remote desktop an application. Now one use want to close the application, but when he close the main application it has to make sure the sub applications are closed.I used process to check the sub applications are closed, but even though my subapplications are closed I always get message as the sub applications are not clsoed,
But when the second user close his sub application I dont' get message.
So how can I check only my user name's process are running

Code Ex :
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)

Thank you

Questionremote desktoping Pinmemberriti kapoor14-Aug-12 6:52 
GeneralMy vote of 5 PinmemberFarhan Ghumra6-Aug-12 22:50 
GeneralMy vote of 5 PinmvpKanasz Robert6-Aug-12 22:48 
QuestionHow i can Remote a computer web connection Pinmemberjprogramer23-Jun-12 23:50 
QuestionRemote system is logging off Pinmemberdarvisunny10-Apr-12 19:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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.150414.1 | Last Updated 11 Nov 2009
Article Copyright 2009 by Thiagarajan Alagarsamy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid