1

I am trying to make an app that creates a shortcut for selected program. When the program starts, it shows all programs in listbox and you can search for the program. How to create a shortcut from selected program inside listbox and name it like selected program. I used this code but I only created a shortcut for notepad. Create shortcut on desktop C#

private void CreateShortcut()
{
    object shDesktop = (object)"Desktop";
    WshShell shell = new WshShell();
    //string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Notepad.lnk";
    string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Notepad.lnk";
    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress );
    shortcut.Description = "New shortcut for a Notepad";
    shortcut.Hotkey = "Ctrl+Shift+N";
    shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolde r.System) + @"\notepad.exe";
    shortcut.Save();
}
Raz Luvaton
  • 2,467
  • 2
  • 21
  • 29
Pavle
  • 13
  • 2
  • 6

2 Answers2

1

You can use CSharpLib. Download it here or, if you're using Visual Studio, enter Install-Package CSharpLib -Version 2.4.5 in Tools > NuGet Package Manager > Package Manager Console. They have a couple methods in the Shortcut class that you can use for shortcut manipulation. For example:

using CSharpLib;

Shortcut shortcut = new Shortcut();
shortcut.CreateShortcutToFile("target_file", "shortcut_file");
S. Ferrell
  • 123
  • 2
  • 7
-1

Change shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolde r.System) + @"\notepad.exe"; to shortcut.TargetPath = YourListBox.getSelected();

EDIT: if getSelected() doesn't work, try getSelectedItem()

Raz Luvaton
  • 2,467
  • 2
  • 21
  • 29
Mine_Stone
  • 159
  • 2
  • 13