/// <summary> /// This method creates a shortcut for a given special folder using given shortcut /// name, description, the fully qualified executable path and a space separated /// list of additional arguments. /// </summary> /// <remarks> /// The given executable's name is used if parameter <paramref name="shortcut"/> /// is not set. /// </remarks> /// <param name="destination"> /// The shortcut's destination. This parameter is <b>mandatory</b>. /// </param> /// <param name="executable"> /// The fully qualified executable path. This parameter is <b>mandatory</b>. /// </param> /// <param name="shortcut"> /// The shortcut name to be used. This parameter is <b>optional</b>. /// </param> /// <param name="description"> /// The description of the shortcut to create. This parameter is <b>optional</b>. /// </param> /// <param name="working"> /// The fully qualified working directory of the application. This parameter is /// <b>optional</b>. /// </param> /// <param name="arguments"> /// The space separated list of additional arguments. This parameter is <b>optional</b>. /// </param> private static void CreateShortcut(Environment.SpecialFolder destination, string executable, string shortcut, string description, string working, string arguments) { if (String.IsNullOrEmpty(executable)) { throw new ArgumentNullException("executable"); } if (!File.Exists(executable)) { throw new FileNotFoundException((new FileNotFoundException()).Message, executable); } // Get shortcut name from executable, if necessary. if (String.IsNullOrEmpty(shortcut)) { shortcut = Path.GetFileNameWithoutExtension(executable); } // Try get the fully qualified path of the shell shortcut file. // This call may cause an exception. Therefore, get this path // before anything else is done. string path = Shortcut.GetLinkFilePath(destination, shortcut); // Create and setup the shell shortcut information. IShellLink link = (IShellLink) new ShellLink(); link.SetPath(executable); if (!String.IsNullOrEmpty(description)) { link.SetDescription(description); } if (!String.IsNullOrEmpty(working)) { link.SetWorkingDirectory(working); } if (!String.IsNullOrEmpty(arguments)) { link.SetArguments(arguments); } // Finally, save current shell shortcut information into its file. IPersistFile file = (IPersistFile)link; file.Save(path, false); }
/// <summary> /// This method removes the shortcut for the given shortcut name from the given /// special folder. /// </summary> /// <param name="destination"> /// The shortcut's destination. This parameter is <b>mandatory</b>. /// </param> /// <param name="shortcut"> /// The shortcut name to remove. This parameter is <b>mandatory</b>. /// </param> private static void RemoveShortcut(Environment.SpecialFolder destination, string shortcut) { // Try get the fully qualified path of the shell // shortcut file. This call may cause an exception. File.Delete(Shortcut.GetLinkFilePath(destination, shortcut)); }
/// <summary> /// This method tries to determine if the given shortcut exists within the given /// special folder. /// </summary> /// <param name="destination"> /// The shortcut's destination. This parameter is <b>mandatory</b>. /// </param> /// <param name="shortcut"> /// The shortcut name to remove. This parameter is <b>mandatory</b>. /// </param> /// <returns> /// This method returns <c>true</c> if a shortcut exists for the combination of /// special folder and shortcut name and <c>false</c> otherwise. /// </returns> private static bool IsShortcut(Environment.SpecialFolder destination, string shortcut) { // Try get the fully qualified path of the shell // shortcut file. This call may cause an exception. return(File.Exists(Shortcut.GetLinkFilePath(destination, shortcut))); }