Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
World Class ASP.NET Hosting - 3 Month Free Hosting, Click Here!
 Resources  
Close
 Our Network  
Close
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.

Total page views :  10791
Total downloads :  655
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
MyRDP.zip
 
Become a Sponsor


Download Free Book


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
 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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
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.
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
MyRDP.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
gggggggggggggggh by lahiru On November 1, 2009
dgggggggggggggggggggg
Reply | Email | Delete | 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 | Delete | 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 | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2010  Mindcracker LLC. All Rights Reserved