Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Create Internet Shortcut

0.00/5 (No votes)
3 Jul 2014 1  
How to create an internet shortcut

Introduction

Did you ever get such a requirement to create the shortcut of your website with query string? Also, user should be able to create the shortcut of website from the page itself. User just needs one button click to have his/her shortcut URL ready on his/her desktop.

It is very easy to create the shortcut through Microsoft's built in utility. At the same time, it is somewhat tricky to create the shortcut of your URL through your website only. As you know, you can't create a shortcut on Client's machine due to security concerns through any web page.

In this article, I will show you how to create a shortcut file and save/download it using Jquery. I have tried my best to provide clean code with comments.

Using the Code

  1. Create one empty website.
  2. Add default page.
  3. Add the below jquery files:
    1. jquery-1.7.1.min
    2. json2-min

HTML Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="js/json2-min.js"></script>
    <script src="js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript">
        function GenerateShortCut() {
            var params = new Object();
            
            params.url = '<%= Request.Url %>';

            $.ajax
                    ({
                        url: "Default.aspx/GenerateShortCut",
                        data: JSON.stringify(params), // For empty input data use "{}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        success: function () { document.getElementById('<%= btnDownload.ClientID %>').click(); },
                        failure: function () { }
                    });
                }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="button" value="Create Shortcut" onclick="GenerateShortCut();" />
            <div style="display:none">
                <asp:Button ID="btnDownload" runat="server" />
            </div>
        </div>
    </form>
</body>
</html>

Default.cs

using System;
using System.IO;
using System.Web;
using System.Web.Services;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnDownload.Click += btnDownload_Click;
    }

    /// <summary>
    /// Download Event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void btnDownload_Click(object sender, EventArgs e)
    {
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", 
        "attachment; filename=test.url"); //Change the file name as per your requirement
        HttpContext.Current.Response.TransmitFile
        (HttpContext.Current.Server.MapPath("~/Temp/test.url")); //Change the file name as per your requirement
        HttpContext.Current.Response.End();
    }

    /// <summary>
    /// Generate the short cut file and store it in temp folder
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    [WebMethod]
    public static string GenerateShortCut(string url)
    {
        try
        {
            // IconFile = URL of favicon.ico file
            string[] lines = { "[InternetShortcut]", "URL=" + url, 
            "IconFile=", "IconIndex=0" };

            //If file exists then delete the old "speedtest.url" file
            if (File.Exists(HttpContext.Current.Server.MapPath("").ToString() + 
            "\\temp\\test.url")) //Change the file name as per your requirement
            {
                File.Delete(HttpContext.Current.Server.MapPath("").ToString() + 
                "\\temp\\test.url"); //Change the file name as per your requirement
            }

            using (System.IO.StreamWriter file = new System.IO.StreamWriter
               ((HttpContext.Current.Server.MapPath("").ToString() + 
               "\\Temp\\test.url"))) //Change the file name as per your requirement
            {
                foreach (string line in lines)
                {
                    file.WriteLine(line);
                }
            }
        }
        catch (Exception ex)
        { 
        }
        return "";
    }
} 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here