Click here to Skip to main content
15,860,943 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.4K   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

 
Questionerror: External component has thrown an exceptio Pin
Member 159985317-May-23 22:43
Member 159985317-May-23 22:43 
QuestionI couldn't run it even though I tried it on different computers. Is there a feature that needs to be installed? I enter the server name as "ip received by the computer" user name "computer name" password "computer password" Pin
Tarkanhan Turhan10-Feb-23 0:47
Tarkanhan Turhan10-Feb-23 0:47 
QuestionWin10|Win 2019 connection ussue Pin
yudinvs9-Feb-22 3:58
yudinvs9-Feb-22 3:58 
QuestionRemote Desktop Web Page Pin
Iyad Moussa23-Aug-21 1:13
Iyad Moussa23-Aug-21 1:13 
QuestionPublic IP Pin
Sanjay kumar126-Nov-20 8:11
Sanjay kumar126-Nov-20 8:11 
QuestionHow to set RDP with Read Only Permission to user Pin
Member 1209011210-Sep-20 2:01
Member 1209011210-Sep-20 2:01 
Questionuser name,server Pin
erummirza10-Jul-20 23:47
erummirza10-Jul-20 23:47 
PraiseThank you so much Pin
Member 1466372121-Nov-19 5:08
Member 1466372121-Nov-19 5:08 
QuestionRe: Thank you so much Pin
Member 1472178710-Feb-20 8:37
Member 1472178710-Feb-20 8:37 
QuestionRun My Software When remote connection desktop user disconnect and connect back Pin
Member 1457282016-Oct-19 21:24
Member 1457282016-Oct-19 21:24 
QuestionPlease Help Me Doesn't work :( Pin
Member 145240809-Jul-19 22:00
Member 145240809-Jul-19 22:00 
QuestionIt Doesn't show a thing Pin
Member 1415214815-Feb-19 18:45
Member 1415214815-Feb-19 18:45 
AnswerRe: It Doesn't show a thing Pin
Member 1449567312-Jun-19 0:44
Member 1449567312-Jun-19 0:44 
AnswerRe: It Doesn't show a thing Pin
Member 127313879-Jul-19 5:47
Member 127313879-Jul-19 5:47 
GeneralRe: It Doesn't show a thing Pin
Member 1453655620-Jul-19 9:19
Member 1453655620-Jul-19 9:19 
GeneralRe: It Doesn't show a thing Pin
Mai Chấn Duy27-Dec-19 7:43
Mai Chấn Duy27-Dec-19 7:43 
GeneralRe: It Doesn't show a thing Pin
Member 152270671-Jun-21 20:56
Member 152270671-Jun-21 20:56 
GeneralRe: It Doesn't show a thing Pin
Member 92447724-Oct-21 7:13
Member 92447724-Oct-21 7:13 
GeneralRe: It Doesn't show a thing Pin
yudinvs9-Feb-22 3:58
yudinvs9-Feb-22 3:58 
QuestionLock and Access Pin
Member 1407954212-Dec-18 20:03
Member 1407954212-Dec-18 20:03 
Questionbest way for use CredSspSupport and connect to 99% of any RDP Pin
dr.pantagon27-Sep-18 1:10
dr.pantagon27-Sep-18 1:10 
QuestionRe: best way for use CredSspSupport and connect to 99% of any RDP Pin
Member 1472178710-Feb-20 8:35
Member 1472178710-Feb-20 8:35 
QuestionHow to start a service inside the remote system Pin
Arunachalam Gurusamy26-Mar-18 2:14
Arunachalam Gurusamy26-Mar-18 2:14 
Questionstart the a program when connected Pin
Member 1361793318-Jan-18 4:21
Member 1361793318-Jan-18 4:21 
Questionhow can i use that code Pin
Member 1331397617-Jul-17 5:56
Member 1331397617-Jul-17 5:56 

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.