Creating a shortcut for a folder in C #

In short, I need to create a shortcut for a folder using C #. I read about using IWshRuntimeLibrary . When I try to use something using IWshRuntimeLibrary , I get all kinds of ambiguity errors with System.IO.File . I assume that this is due to the fact that there is an IWshRuntimeLibrary.File and System.IO.File interface. All I really managed to find was articles about creating shortcuts for applications, not folders.

Deviations from ambiguity:

  • Is this used correctly for folder hotkeys?
  • How to create a shortcut using this
  • How to indicate where the shortcut is

Also, when I try to create a shortcut for a folder, say C:\TEMP using this:

 IWshShortcut shortcut; wshShell = new WshShellClass(); shortcut = (IWshShortcut)wshShell.CreateShortcut(@"C:\TEMP"); shortcut.TargetPath = @"C:\Documents and Settings"; 

I get a COMException . According to what I read, this should create a shortcut for the temporary drive folder C and put the shortcut in the documents and settings.

+7
source share
3 answers

Remember to set Embed Interop Types to False to reference Interop.IWshRuntimeLibrary. I tested and works without errors.

  // Make sure you use try/catch block because your App may has no permissions on the target path! try { CreateShortcut(@"C:\temp", @"C:\MyShortcutFile.lnk", "Custom Shortcut", "/param", "Ctrl+F", @"c:\"); } catch (Exception ex) { Console.WriteLine(ex.Message); } /// <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 static 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 static void CreateShortcut(string TargetPath, string ShortcutFile, string Description, string Arguments, string HotKey, string WorkingDirectory) { // Check necessary parameters first: if (String.IsNullOrEmpty(TargetPath)) throw new ArgumentNullException("TargetPath"); if (String.IsNullOrEmpty(ShortcutFile)) throw new ArgumentNullException("ShortcutFile"); // Create WshShellClass instance: var wshShell = new WshShellClass(); // Create shortcut object: IWshRuntimeLibrary.IWshShortcut shorcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(ShortcutFile); // Assign shortcut properties: shorcut.TargetPath = TargetPath; 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(); } 

source: http://zayko.net/post/How-to-create-Windows-shortcut- (C) .aspx

+7
source

You have the opposite: you are trying to make C:\Temp shortcut for your C:\Documents and Settings folder.

The following snippet will create a shortcut for the network folder on the desktop:

 var desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); IWshShortcut shortcut; var wshShell = new WshShellClass(); shortcut = (IWshShortcut)wshShell.CreateShortcut(Path.Combine(desktop, @"Temp.lnk")); shortcut.TargetPath = @"\\computername\sharename"; shortcut.Save(); 
+3
source

Solution using IShellLink to exchange experts:

Create shortcuts in .NET

Provides a managed layer for creating file and file shorcuts.

This is VB.NET, but you can convert it to C # using free converters.

+1
source

All Articles