internal ShortcutInfo(string path, IShellLink native) { Path = path; StringBuilder tmp = new StringBuilder(1024); native.GetArguments(tmp, tmp.Capacity); Arguments = tmp.ToString(); native.GetDescription(tmp, tmp.Capacity); Description = tmp.ToString(); native.GetIconLocation(tmp, tmp.Capacity, out IconIndex); IconPath = tmp.ToString(); native.GetPath(tmp, tmp.Capacity, IntPtr.Zero, 0); TargetPath = tmp.ToString(); native.GetWorkingDirectory(tmp, tmp.Capacity); WorkingDirectory = tmp.ToString(); uint ws = 0; native.GetShowCmd(out ws); WindowState = (ShortcutWindowState)(ws); }
/// <summary> /// Check if a specified shortcut file exists. /// </summary> /// <param name="shortcutPath">Shortcut file path</param> /// <param name="targetPath">Target file path of shortcut</param> /// <param name="arguments">Arguments of shortcut</param> /// <param name="comment">Comment of shortcut</param> /// <param name="workingFolder">Working folder of shortcut</param> /// <param name="windowState">Window state of shortcut</param> /// <param name="iconPath">Icon file path of shortcut</param> /// <param name="appId">AppUserModelID of shortcut</param> /// <returns>True if exists</returns> public bool CheckShortcut( string shortcutPath, string targetPath, string arguments, string comment, string workingFolder, ShortcutWindowState windowState, string iconPath, string appId) { if (!File.Exists(shortcutPath)) return false; using (var shellLink = new ShellLink(shortcutPath)) { // File path casing may be different from that when installed the shortcut file. return (shellLink.TargetPath.IsNullOrEmptyOrEquals(targetPath, StringComparison.OrdinalIgnoreCase) && shellLink.Arguments.IsNullOrEmptyOrEquals(arguments, StringComparison.Ordinal) && shellLink.Description.IsNullOrEmptyOrEquals(comment, StringComparison.Ordinal) && shellLink.WorkingDirectory.IsNullOrEmptyOrEquals(workingFolder, StringComparison.OrdinalIgnoreCase) && (shellLink.WindowStyle == ConvertWindowState(windowState)) && shellLink.IconPath.IsNullOrEmptyOrEquals(iconPath, StringComparison.OrdinalIgnoreCase) && shellLink.AppUserModelID.IsNullOrEmptyOrEquals(appId, StringComparison.Ordinal)); } }
/// <summary> /// Delete a specified shortcut file. /// </summary> /// <param name="shortcutPath">Shortcut file path</param> /// <param name="targetPath">Target file path of shortcut</param> /// <param name="arguments">Arguments of shortcut</param> /// <param name="comment">Comment of shortcut</param> /// <param name="workingFolder">Working folder of shortcut</param> /// <param name="windowState">Window state of shortcut</param> /// <param name="iconPath">Icon file path of shortcut</param> /// <param name="appId">AppUserModelID of shortcut</param> /// <remarks>If contents of shortcut do not match, the shortcut file will not be deleted.</remarks> public void DeleteShortcut( string shortcutPath, string targetPath, string arguments, string comment, string workingFolder, ShortcutWindowState windowState, string iconPath, string appId) { if (!CheckShortcut( shortcutPath: shortcutPath, targetPath: targetPath, arguments: arguments, comment: comment, workingFolder: workingFolder, windowState: windowState, iconPath: iconPath, appId: appId)) return; File.Delete(shortcutPath); }
/// <summary> /// Install a specified shortcut file. /// </summary> /// <param name="shortcutPath">Shortcut file path</param> /// <param name="targetPath">Target file path of shortcut</param> /// <param name="arguments">Arguments of shortcut</param> /// <param name="comment">Comment of shortcut</param> /// <param name="workingFolder">Working folder of shortcut</param> /// <param name="windowState">Window state of shortcut</param> /// <param name="iconPath">Icon file path of shortcut</param> /// <param name="appId">AppUserModelID of shortcut</param> /// <param name="activatorId">AppUserModelToastActivatorCLSID of shortcut</param> public void InstallShortcut( string shortcutPath, string targetPath, string arguments, string comment, string workingFolder, ShortcutWindowState windowState, string iconPath, string appId, Guid activatorId) { if (string.IsNullOrWhiteSpace(shortcutPath)) { throw new ArgumentNullException(nameof(shortcutPath)); } using (var shellLink = new ShellLink { TargetPath = targetPath, Arguments = arguments, Description = comment, WorkingDirectory = workingFolder, WindowStyle = ConvertToWindowStyle(windowState), IconPath = iconPath, IconIndex = 0, // The first icon in the file AppUserModelID = appId, }) { if (activatorId != Guid.Empty) { shellLink.AppUserModelToastActivatorCLSID = activatorId; } shellLink.Save(shortcutPath); } }
/// <summary> /// Install a specified shortcut file. /// </summary> /// <param name="shortcutPath">Shortcut file path</param> /// <param name="targetPath">Target file path of shortcut</param> /// <param name="arguments">Arguments of shortcut</param> /// <param name="comment">Comment of shortcut</param> /// <param name="workingFolder">Working folder of shortcut</param> /// <param name="windowState">Window state of shortcut</param> /// <param name="iconPath">Icon file path of shortcut</param> /// <param name="appId">AppUserModelID of shortcut</param> public void InstallShortcut( string shortcutPath, string targetPath, string arguments, string comment, string workingFolder, ShortcutWindowState windowState, string iconPath, string appId) { if (String.IsNullOrWhiteSpace(shortcutPath)) throw new ArgumentNullException("shortcutPath"); using (var shellLink = new ShellLink { TargetPath = targetPath, Arguments = arguments, Description = comment, WorkingDirectory = workingFolder, WindowStyle = ConvertWindowState(windowState), IconPath = iconPath, IconIndex = 0, // 1st icon in the file AppUserModelID = appId, }) { shellLink.Save(shortcutPath); } }
private static ShellLink.SW ConvertWindowState(ShortcutWindowState windowState) { switch (windowState) { case ShortcutWindowState.Maximized: return ShellLink.SW.SW_SHOWMAXIMIZED; case ShortcutWindowState.Minimized: return ShellLink.SW.SW_SHOWMINNOACTIVE; default: return ShellLink.SW.SW_SHOWNORMAL; } }