posts - 31, comments - 85, trackbacks - 0

My Links

News

Archives

Post Categories

Tuesday, March 17, 2009

Hyperlink to Windows Remote Desktop (RDP) session using ASP.NET

Hyperlink to Remote Desktop (RDP) session:
I had some IP addresses on a web page which would be used internally by our company users and wanted to display them as hyperlinks. When a user clicks an IP address, a Windows RDP session should start.
The simplest way I could think of was to transfer an RDP file to the client, which she can open or save via regular HTTP File Transfer.
An RDP file can be edited by a simple text editor. If you open an RDP file with notepad, you will see all the settings. The one we are interested in is full address:s:
I use ASP.NET LinkButton control as the hyperlink and use the Text property of the LinkButton to modify the full address:s: setting.
My code uses a Default.rdp file located in Download directory in the root of my web site. I open the file, modify the full address property, save it and transfer the file to the client.
Here’s the piece of code. I agree it is not polished yet, but I know you can do it.
C# Code:
 protected void LinkButton_Click(object sender, EventArgs e)
    {
        // Default RDP file
        string fileName = "Default.rdp";
       
        if ((fileName != ""))
        {
            // Get absolute path of the file
            string filePath = Server.MapPath("~/Download/" + fileName);
 
            // Update RDP file
            LinkButton linkButton = (LinkButton)sender;
            UpdateRDPFile(linkButton.Text, filePath);
 
            // Download RDP file
            DownLoadFile(filePath);
        }
        else
        {
            Response.Write("Please provide a file to download.");
        }
    }
 
    private void UpdateRDPFile(string IPAddress, string filePath)
    {
        StringBuilder newFile = new StringBuilder();
        string temp = "";
        string[] fileToUpdate = File.ReadAllLines(filePath);
        foreach (string line in fileToUpdate)
        {
            if (line.Contains("full address:s:"))
            {
                temp = temp.Insert(0, "full address:s:" + IPAddress);
                newFile.Append(temp + "\r\n");
                continue;
            }
            newFile.Append(line + "\r\n");
        }
 
        File.WriteAllText(filePath, newFile.ToString());
    }
 
    private void DownLoadFile(string filePath)
    {
        // get file object as FileInfo
        System.IO.FileInfo file = new System.IO.FileInfo(filePath);
        // -- if the file exists on the server
        if (file.Exists)
        {
            // set appropriate headers
            Response.Clear();
            Response.AddHeader("Content-Disposition", ("attachment; filename=" + file.Name));
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
            // if file does not exist
        }
        else
        {
            Response.Write("This file does not exist.");
        }       
    }
 
ASPX Markup:
<table style="border: 1px solid #BBB; border-collapse: collapse; width: 520px;" rules="all"
                cellspacing="0" cellpadding="3">
                <tr style="background-color: #FF9900; color: #FFF">
                    <td colspan="2">
                        RDP
                    </td>
                </tr>
                <tr style="background-color: #ECECEC; color: #000">
                    <td>
                        Description
                    </td>
                    <td>
                        Link
                    </td>
                </tr>
                <tr>
                    <td>
                        RDP1:
                    </td>
                    <td>
                        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton_Click">127.0.0.1</asp:LinkButton>
                    </td>
                </tr>
                <tr>
                    <td>
                        RDP2:
                    </td>
                    <td>
                        <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton_Click">127.0.0.2</asp:LinkButton>
                    </td>
                </tr>
                <tr>
                    <td>
                        RDP3:
                    </td>
                    <td>
                        <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton_Click">127.0.0.3</asp:LinkButton>
                    </td>
                </tr>
            </table>
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Tuesday, March 17, 2009 1:54 AM | Feedback (3) | Filed Under [ C# ASP.NET ]

Powered by: