ARTICLE

Remote Desktop Using C#.Net

Posted by Thiagarajan Alagarsamy Articles | Windows Forms C# October 26, 2009
Remote Desktop Services is one of Microsoft Windows components to access a remote computer through 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.
Reader Level:
Download Files:
 

Overview:

Remote Desktop Services is one of Microsoft Windows components to access a remote computer through 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 homeJ).

1.JPG

Why do we 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.

However, tab control is not in scope of this article. That gives me an idea for my next article :)

Microsoft Terminal Services Control

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 reference to "Microsoft Terminal Services Control Type Library" from the COM tab. This will add MSTSCLib.dll to the project.

2.JPG

To add MSTSC to the toolbox, right click the toolbox and select "Choose Items…". Now add "Microsoft Terminal Services control from the COM tab.


3.JPG

Drag the newly added control from toolbox to the form.

Add 3 textbox and 2 button controls to the form.

4.JPG

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

 

if (rdp.Connected.ToString() == "1")

   rdp.Disconnect();


Check the full source code in the attachment. Please leave your valuable comments and questions.

That's all folks! Happy Coding!

Login to add your contents and source code to this article
post comment
     

This code is not working on VB.Net 2010 version and windows 7...Please help me to do the same on VB.NET 2010 or mail me the code for VB.NET 2010 in the id: arup_dolui@yahoo.com. Thanx in advance.

Posted by Arup Dolui Sep 13, 2012

Is there a way to write something in c# that redirects the Windows command line? I don't need the gui, sometimes I need to just log in to a Windows box, do one thing, and get out. Client doesn't want ssh server, so I'm looking to write something quick (client/server). In Linux we redirect the tty - how do I do this on Windows?

Posted by Kathy S Apr 13, 2012

Dear Thiagarajan Alagarsamy, When we run this application remote computer is logging off.Do you have any solution for this.

Posted by darvi sunny Apr 10, 2012

anybody help me to create interface for above project

Posted by aamir akhter Mar 27, 2012

Thanks for this tutorial This is something I was looking for a long time

Posted by James Wijaya Feb 02, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts