Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Mindcracker MVP Summit 2012
Search :       Advanced Search »
Home » COM Interop » Remote Desktop using C#.Net

Remote Desktop using C#.Net

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.

Page Views : 58267
Downloads : 3231
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
MyRDP.zip
 
 
Mindcracker MVP Summit 2012
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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!

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Thiagarajan Alagarsamy
Thiagu is living in Bangalore, India. His native is Madurai, a historic city in south India. He loves to code in C#. He frequents c# corner articles when he is not coding. Thiagu loves reading Jeffrey Archer and Sidney Sheldon novels. He is very much interested in Artificial Intelligence (AI). To view his blog - http://csharpnet.blogspot.com
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Mindcracker MVP Summit 2012
Become a Sponsor
 Comments
gggggggggggggggh by lahiru On November 1, 2009
dgggggggggggggggggggg
Reply | Email | Modify 
Great tutorial by Benjamin On November 13, 2009
Thank you very much for this tutorial!

I'd just like to add, what if you want to login using a domain-login, you have to specify the username as DOMAINNAME\USERNAME.

On top of that, this method will even login if the PC is "secured" by you having to press CTRL-ALT-DEL in order to login.
Reply | Email | Modify 
I m love in it by Muhammad On December 3, 2009
This is something I was looking for a long time. As I need to work on multiple servers in my office through RDP. I will share this with my colleages too.
Thanks for sharing this.
I am love'in it.

Reply | Email | Modify 
RDP Project by deep On March 5, 2010
Hi there
I have tried to run your application but it does not work..I couldn't see any bug on this project but couldn't connect to remote computer as well...when i press connect..it doesn't show any reaction nor it display any error..so can u plz help me running this application...

Thank X
Reply | Email | Modify 
missing Axhost after using axImp.exe by adma On April 17, 2010
This project references to AxMSTSCLib.dll (which one of his memebres is  AXHost)

I have used the command axImp.exe to create a AxMSTSCLib.dll:
%<SDK bin directory>%\aximp.exe %windir%\system32\mstscax.dll

But there is no Axhost (checked the object browser through visual studio).
so I guess I'm creating this dll the wrong way, but I could find any other way.


here is my dll compared with the one which was donloaded from code-porject
http://drop.io/hidden/eopb4tphk8qekl/asset/Z2V0b2N4LTItanBn
http://drop.io/hidden/eopb4tphk8qekl/asset/bm8tZ2V0LW9jeDItanBn

Anyone knows why? and how can I fix that? or where should i ask this ver specific question?


Regards,
Elad
Reply | Email | Modify 
Connect pda desktop from desktop pc through remote desktop by Muhammad On April 23, 2010
I am writing an application that handle mobile desktops PDAs from desktop pc via remote desktop.my application is working well from desktop to desktop but i want to connect from desktop to handle mobile desktop [PDA].
Can you please help me.
Reply | Email | Modify 
Hi by Tejaswini On May 12, 2010
Hi,
   can u tell me how I can find from an application whether user is using RDP?

Reply | Email | Modify 
very nice by balu On May 13, 2010
hi the given sample is very nice.
first time when i run the application i am unable to get the remote desktop in the application after i given the permission in the server "Allow remote desktop" in the remote tab of my computer properties, then i am able to view the remote desktop.

thank you its really good.
Reply | Email | Modify 
Can we capture the data and tunnel through sockets by Siva On May 27, 2010
I want to capture the RDP data and sent it to server using sockets (instead of showing it in FORM),where server can show that in a Java Applet.

Is there any way to do like this?

Thanks,
Siva.
Reply | Email | Modify 
To View Remote user without getting logout by Anandhi On February 10, 2011
On connecting to the remote system, while connection is established automatically the remote system is getting log off. Can we recover this problem? that is while connecting to the remote system that system should not log off. Is there any idea to do this.. Thanks in advance.
Reply | Email | Modify 
How can we use it in Web Application? by smileoftushar On February 11, 2011
I tried to add the MSTSC confrol into the toolbox but nothing happens. Does this code work only in Windows app?
Reply | Email | Modify 
how to use with a different port number by Jaiprakash On February 12, 2011
Hi, how do i connect using different port, my server's default terminal port is changed to 1978 i tried using server1:1978 but getting error Please advice on the same Thanks
Reply | Email | Modify 
3q by QING On February 19, 2011
thank you! could this program support win 7?
Reply | Email | Modify 
sending detail of remote installer by ravi On March 16, 2011
i want that kind of fiture in coding complite my all expectations
Reply | Email | Modify 
This is Great! by David On August 9, 2011
Very cool short and simple program. Do you know of a way to implement file transfer into this program? Thanks, David
Reply | Email | Modify 
MyRdp.zip by Nagendra On September 8, 2011
we success to connect remote computer but remote computer locked. how can i unlock remote computer. Reply me very soon it is urgent
Reply | Email | Modify 
Microsoft Terminal Services Control Type Library is missing by jeet On December 17, 2011
I didn't find the above reference in my COM component. Please tell me where i can get it
Reply | Email | Modify 
Very Useful Information by James On February 2, 2012
Thanks for this tutorial This is something I was looking for a long time
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.