1. First of all, add reference to Windows Script Host Object model:
2. Add Namespace:
3. Add the code below to your App. Note: I didn't assign all IWshShortcut properties just mostly used so please check all properties by yourself.
/// <summary>
/// Create Windows Shorcut
/// </summary>
/// <param name="SourceFile">A file you want to make shortcut to</param>
/// <param name="ShortcutFile">Path and shorcut file name including file extension (.lnk)</param>
public void CreateShortcut(string SourceFile, string ShortcutFile)
{
CreateShortcut(SourceFile, ShortcutFile, null, null, null, null);
}
/// <summary>
/// Create Windows Shorcut
/// </summary>
/// <param name="SourceFile">A file you want to make shortcut to</param>
/// <param name="ShortcutFile">Path and shorcut file name including file extension (.lnk)</param>
/// <param name="Description">Shortcut description</param>
/// <param name="Arguments">Command line arguments</param>
/// <param name="HotKey">Shortcut hot key as a string, for example "Ctrl+F"</param>
/// <param name="WorkingDirectory">"Start in" shorcut parameter</param>
public void CreateShortcut(string SourceFile, string ShortcutFile, string Description,
string Arguments, string HotKey, string WorkingDirectory)
{
// Check necessary parameters first:
if (String.IsNullOrEmpty(SourceFile))
throw new ArgumentNullException("SourceFile");
if (String.IsNullOrEmpty(ShortcutFile))
throw new ArgumentNullException("ShortcutFile");
// Create WshShellClass instance:
WshShellClass wshShell = new WshShellClass();
// Create shortcut object:
IWshRuntimeLibrary.IWshShortcut shorcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(ShortcutFile);
// Assign shortcut properties:
shorcut.TargetPath = SourceFile;
shorcut.Description = Description;
if(!String.IsNullOrEmpty(Arguments))
shorcut.Arguments = Arguments;
if (!String.IsNullOrEmpty(HotKey))
shorcut.Hotkey = HotKey;
if (!String.IsNullOrEmpty(WorkingDirectory))
shorcut.WorkingDirectory = WorkingDirectory;
// Save the shortcut:
shorcut.Save();
}
4. And here is how to call function you just added:
// Make sure you use try/catch block because your App may has no permissions on the target path!
try
{
CreateShortcut(@"C:\MySourceFile.exe", @"C:\MyShortcutFile.lnk",
"Custom Shortcut", "/param", "Ctrl+F", @"c:\");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}