Click here to Skip to main content
Click here to Skip to main content

Create Internet Shortcut

, 3 Jul 2014
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Narotam Gediya
Team Leader
India India
No Biography provided

Comments and Discussions

 
Generalinteresting! PinprofessionalBrian A Stephens18-Jul-14 7:52 
GeneralMy vote of 5 PinmemberHumayun Kabir Mamun3-Jul-14 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140718.1 | Last Updated 3 Jul 2014
Article Copyright 2014 by Narotam Gediya
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid