Click here to Skip to main content
6,822,613 members and growing! (18,452 online)
Email Password   helpLost your password?
Languages » C# » COM Interop     Intermediate License: The Code Project Open License (CPOL)

Remote Desktop using C#.NET

By Thiagarajan Alagarsamy

This article is about showing how to create a .NET application to perform remote desktop operation using Microsoft Terminal Services Client ActiveX control.
C#, Windows, .NET, Dev
Posted:11 Nov 2009
Views:13,433
Bookmarked:85 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 5.11 Rating: 4.07 out of 5

1
1 vote, 5.6%
2
7 votes, 38.9%
3

4
10 votes, 55.6%
5

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.

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.

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")
 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)

About the Author

Thiagarajan Alagarsamy


Member
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

Occupation: Web Developer
Location: India India

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
GeneralThanks but why not using AdvancedSettings2 PinmemberKam12:17 9 Jan '10  
GeneralHi! Nice article PinmemberVirat Kothari9:08 17 Nov '09  
GeneralVery nice -- thanks! PinmemberBit-Smacker8:21 17 Nov '09  
GeneralTried this is Access 2007 VBA Pinmemberdpminusa19:02 16 Nov '09  
GeneralI've done this before myself, and there's RDTabs if you want it PinmemberMichael Adams16:57 16 Nov '09  
QuestionThis looks very similar to an earlier article. Pinmemberdpminusa16:27 16 Nov '09  
GeneralWhat about Terminals? PinmemberGarth Arnold10:29 16 Nov '09  
GeneralQuick and to the point! PinmemberTomB10:23 16 Nov '09  
GeneralWhat about mRemote? PinmemberMember 17809899:00 12 Nov '09  
GeneralRe: What about mRemote? PinmemberThiagarajan Alagarsamy20:35 12 Nov '09  
Generalsimple.. Pinmembersashidhar6:07 12 Nov '09  
GeneralRe: simple.. PinmemberThiagarajan Alagarsamy20:34 12 Nov '09  
GeneralMy vote of 2 PinmemberSaravanan Chinnusamy20:24 11 Nov '09  
GeneralRe: My vote of 2 PinmemberThiagarajan Alagarsamy0:10 12 Nov '09  
AnswerRe: My vote of 2 PinmemberBit-Smacker8:18 17 Nov '09  
GeneralHI PinmemberRavenet16:31 11 Nov '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 11 Nov 2009
Editor: Deeksha Shenoy
Copyright 2009 by Thiagarajan Alagarsamy
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project