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
  • 3,166
  • 4
  • 21
  • 36
Pavle
  • 13
  • 2
  • 6
  • you should show the code that doesn't works – McNets Nov 24 '16 at 18:31
  • Also, you are probably going to need to pass some arguments to that function, else how will it know what program it's meant to be adding? – ScottishTapWater Nov 24 '16 at 18:34
  • Here is my program source code: http://pastebin.com/Fy0CM0EU – Pavle Nov 24 '16 at 18:57
  • you're using a private session. Please take a look at: http://stackoverflow.com/help/how-to-ask – McNets Nov 24 '16 at 19:07
  • Sorry for that,I changed it now to public!!! – Pavle Nov 24 '16 at 21:09
  • This question is really about using a form control selection as a parameter for creating a shortcut rather than how to create the shortcut itself, which is already implemented. The linked code should be edited into the question as well as a description of how it's lacking or not working. – Lance U. Matthews Sep 27 '22 at 18:52

2 Answers2

2

You can use CSharpLib. Download it here or, if you're using Visual Studio, enter Install-Package CSharpLib -Version 4.0.0 in Tools > NuGet Package Manager > Package Manager Console. It has 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
  • 171
  • 3
  • 11
-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
  • 3,166
  • 4
  • 21
  • 36
Mine_Stone
  • 169
  • 3
  • 13