Click here to Skip to main content
13,195,710 members (53,972 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 22 Nov 2006

Add User Shortcuts To Files/URLs to Application Menus

, 22 Nov 2006
Rate this:
Please Sign up or sign in to vote.
.NET Class that loads a list of files/URLs and displays them in the menu of Windows Forms application
Sample Image - screen0.jpg


UserMenuShortcuts is a .NET class for Windows Forms that displays a list of files or URLs in a menu. When the user clicks an item from the menu, the file/URL is launched on their machine (such as Notepad.exe or Clicked items which reference a file on the user's system will launch the item with its associated application with .NET's Process.Start() method. If the clicked item is a URL, then the user's default browser will be launched and navigate to the specified URL address.

The file that stores the user menu items is the UserShortcuts.xml file, and it is stored in the host application's directory. If a UserShortcuts.xml file is not provided/found, it will be automatically generated whenever the host application runs and cannot find the file.

Using the UserMenuShortcuts .NET Class

To use the UserMenuShortcuts in a Windows Form or another Windows .NET control, add a menu item to the form that will be replaced by the loaded menu shortcuts and then include the UserMenuShortcuts class into the project. Add the click-event handler to the placeholder menu item and then make the following method calls in your form/control class:

private void Form1_Load(object sender, EventArgs e)

private void userItemsPlacehoderToolStripMenuItem_Click(object sender, EventArgs e)
    //NOTE: To override this action, remove the call to "LaunchUserShortcutItem"
    //and access the clicked item by:
    //      Convert.ToString(UserShortcuts.UserShortcutItems[sender.ToString()]);


How the UserMenuShortcuts Class Works

The UserMenuShortcuts class loads a hashtable of the menu items from the UserShortcuts.xml file and then creates a set of menu items at the same level as the placeholder menu item. The Key of each hashtable item is also used as the display text in the menu item, while the Value of the hashtable item is the target file/URL. When the menu item is clicked, a lookup of the clicked menu item's text is performed to retrieve the target file/URL, and then the target is launched with Process.Start().

public class UserShortcuts
    private static Hashtable m_UserShortcutItems = new Hashtable();
    public static Hashtable UserShortcutItems
        get { return m_UserShortcutItems; }
        set { m_UserShortcutItems = value; }

    /// <span class="code-SummaryComment"><summary></span>
    /// Reads the UserShortcutItems file and loads into the specified menu item
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><param name="mniUserShortcutsPlaceholder"></param></span>
    /// <span class="code-SummaryComment"><param name="mniUserShortcutsPlaceholder_Click"></param></span>
    public static void Load(ToolStripMenuItem mniUserShortcutsPlaceholder,
                            EventHandler mniUserShortcutsPlaceholder_Click)
        // generate a sample file if it doesn't exist
        if (File.Exists(_Uri) == false)

        XmlDocument _Document = null;

        // all files are relative to the EXE file
        string sOldWorkingDirectory = Directory.GetCurrentDirectory();

            _Document = new XmlDocument();

            m_UserShortcutItems = new Hashtable();

            XmlNode root = _Document.DocumentElement;
            XPathNavigator nav = root.CreateNavigator();
            XPathNodeIterator nodeIterator = nav.Select("/UserShortcuts/UserShortcut");

            while (nodeIterator.MoveNext())
                string sMenuLabel = nodeIterator.Current.SelectSingleNode
                string sShortcutPath = nodeIterator.Current.SelectSingleNode

                if (System.Uri.IsWellFormedUriString
                    (sShortcutPath, UriKind.Absolute) == false)
                    FileInfo fiInfo = new FileInfo(sShortcutPath);
                    sShortcutPath = fiInfo.FullName;

                if (m_UserShortcutItems.ContainsKey(sMenuLabel) == false)
                    m_UserShortcutItems.Add(sMenuLabel, sShortcutPath);

                 (mniUserShortcutsPlaceholder, mniUserShortcutsPlaceholder_Click);
        catch (Exception ex)
            throw new Exception("Error Loading User Shortcuts", ex);

    /// <span class="code-SummaryComment"><summary></span>
    /// Load the menu items into the placeholder's location and
    /// then delete the placeholder item
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><param name="mniUserShortcutsPlaceholder"></param></span>
    private static void LoadUserShortcutsMenu
        (ToolStripMenuItem mniUserShortcutsPlaceholder,
                   EventHandler mniUserShortcutsPlaceholder_Click)
        if (mniUserShortcutsPlaceholder == null)

        int iplaceholderloc = mniUserShortcutsPlaceholder.Owner.Items.IndexOf

        IDictionaryEnumerator iusershortcuts =
        while (iusershortcuts.MoveNext())
            string sMenuName = Convert.ToString(iusershortcuts.Key);
            ToolStripMenuItem mni = new ToolStripMenuItem(sMenuName);
            mni.Click += new EventHandler(mniUserShortcutsPlaceholder_Click);
            mniUserShortcutsPlaceholder.Owner.Items.Insert(iplaceholderloc, mni);
            iplaceholderloc += 1;


    /// <span class="code-SummaryComment"><summary></span>
    /// Launches the specified value/path/URL of the specified key (menu item)
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><param name="sKey"></param></span>
    public static void LaunchUserShortcutItem(string sKey)
        string sShortcutPath = Convert.ToString(UserShortcuts.UserShortcutItems[sKey]);
        if ((System.Uri.IsWellFormedUriString(sShortcutPath, UriKind.Absolute) == true)
            || (File.Exists(sShortcutPath) == true))
            ProcessStartInfo psi = new ProcessStartInfo(sShortcutPath);
            psi.UseShellExecute = true;


I hope you find this article and class useful - it has come in handy in several applications already!


  • 22nd November, 2006: Article posted


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chris Hambleton
Software Developer
United States United States
SOFTWARE: Chris Hambleton is a Software Developer with proven experience in developing both web and Windows client-server applications with WPF, ASP.NET, C#, SQL Server, VB.NET, Visual C++, and VB6.

Chris's website is at and he has a small web-hosting/consulting business called He has several other websites such as,, and

WRITING: He has also written several fiction books ("The Time of Jacob's Trouble" and "Endeavor in Time"), available at and of course, at (Full Amazon Profile).

You may also be interested in...


Comments and Discussions

GeneralNice! Pin
Alexey A. Popov23-Nov-06 21:41
memberAlexey A. Popov23-Nov-06 21:41 
GeneralFormat Code Pin
Gh0stman23-Nov-06 2:13
memberGh0stman23-Nov-06 2:13 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171019.1 | Last Updated 22 Nov 2006
Article Copyright 2006 by Chris Hambleton
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid