Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#
Article

Remote Desktop using C#.NET

Rate me:
Please Sign up or sign in to vote.
4.94/5 (85 votes)
11 Nov 2009CPOL2 min read 1.2M   88.5K   283   171
This article is about showing how to create a .NET application to perform remote desktop operation using Microsoft Terminal Services Client ActiveX control.
Image 1

Introduction

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.

Background

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.

C#
rdp.Server = txtServer.Text;
rdp.UserName = txtUserName.Text;
IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = txtPassword.Text;
rdp.Connect();

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.

C#
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).

C#
if (rdp.Connected.ToString() == "1")
 rdp.Disconnect();

That’s all folks!

History

  • 5th November, 2009: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
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 - http://csharpnet.blogspot.com

Comments and Discussions

 
QuestionCompatible with Windows 8? Pin
Member 91121273-Oct-13 20:47
Member 91121273-Oct-13 20:47 
AnswerRe: Compatible with Windows 8? Pin
Justin Nichsolson11-Mar-14 19:44
Justin Nichsolson11-Mar-14 19:44 
GeneralRe: Compatible with Windows 8? Pin
Member 1331397620-Jul-17 19:51
Member 1331397620-Jul-17 19:51 
Questioncan we writing such app with mvc? if yes, can you share the code Pin
Member 84904061-Sep-13 23:30
Member 84904061-Sep-13 23:30 
GeneralMy vote of 5 Pin
wsc09189-Jul-13 16:59
wsc09189-Jul-13 16:59 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey4-Jul-13 2:17
professionalManoj Kumar Choubey4-Jul-13 2:17 
QuestionRemote Desktop without GUI Pin
nirali133-Jun-13 20:39
nirali133-Jun-13 20:39 
QuestionUrgent. Pin
Member 919961929-Apr-13 9:40
Member 919961929-Apr-13 9:40 
Can someone Please tell me about the rdp? It says "Error The name 'rdp' does not exist in the current context".
AnswerRe: Urgent. Pin
Christopher Smit17-Oct-13 6:32
Christopher Smit17-Oct-13 6:32 
Question? Pin
WHITELIONX26-Apr-13 19:57
WHITELIONX26-Apr-13 19:57 
AnswerRe: ? Pin
Justin Nichsolson11-Mar-14 19:42
Justin Nichsolson11-Mar-14 19:42 
QuestionPort number Pin
yunuskose22-Apr-13 1:22
yunuskose22-Apr-13 1:22 
AnswerRe: Port number Pin
kodette22-Aug-13 20:34
kodette22-Aug-13 20:34 
QuestionRDP for console Pin
ali_heidari_20-Apr-13 9:15
ali_heidari_20-Apr-13 9:15 
QuestionUnable to connect to wireless laptop which is in same network connected to wireless Pin
Sivanandan16-Apr-13 5:18
Sivanandan16-Apr-13 5:18 
GeneralMy vote of 5 Pin
banerjeedebasish28-Feb-13 23:28
banerjeedebasish28-Feb-13 23:28 
BugCant connect using PORT Pin
venkatmca00813-Feb-13 21:15
professionalvenkatmca00813-Feb-13 21:15 
GeneralRe: Cant connect using PORT Pin
Armin Rezayati29-Apr-14 22:56
Armin Rezayati29-Apr-14 22:56 
BugRemote screen is not displayed Pin
thakurajay9330-Jan-13 18:36
thakurajay9330-Jan-13 18:36 
Bugbut it is not showing the desktop screen after click on connect Pin
bhupendra8816-Dec-12 5:54
bhupendra8816-Dec-12 5:54 
QuestionHow can I logout and NOT Disconnect? Pin
Member 817647311-Nov-12 21:20
Member 817647311-Nov-12 21: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 8:38
tejas.chudasama2-Nov-12 8: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 22:27
Adrian Viegas7-Nov-12 22:27 
Questionc# remote desktop managing application Pin
Member 328966114-Sep-12 4:04
Member 328966114-Sep-12 4:04 
Questionremote desktoping Pin
riti kapoor14-Aug-12 5:52
riti kapoor14-Aug-12 5:52 

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.