public static string GetShortcutTargetFile(string shortcutFullPath) { string path = null; using (ShellLink shellLink = new ShellLink(shortcutFullPath)) { path = shellLink.TargetPath; } return(path); }
/// <summary> /// ONLY USE when application is installed /// </summary> public static void CreateShortcutIconAtPath(string directoryPath) { string fullAppPath = Constants.APPLICATION_FULL_PATH; string iconPath = Path.Combine(Path.GetDirectoryName(fullAppPath), "icon.ico"); var shortcutLocation = Path.Combine(directoryPath, Path.GetFileNameWithoutExtension(fullAppPath) + ".lnk"); if (File.Exists(iconPath) == false) { //copy icon Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("SimpleCopyPasteTool.Resources.Images.icon.ico"); FileStream fileStream = new FileStream(iconPath, FileMode.CreateNew); for (int i = 0; i < stream.Length; i++) { fileStream.WriteByte((byte)stream.ReadByte()); } fileStream.Close(); } if (File.Exists(shortcutLocation)) { //if the existing icon path is not the same as the current application install path (happens with older versions) string targetFullPath = GetShortcutTargetFile(shortcutLocation); if (StringUtils.NormalizePath(targetFullPath) != StringUtils.NormalizePath(Constants.INSTALL_FULLPATH)) { File.Delete(shortcutLocation); } } if (File.Exists(shortcutLocation) == false) { //create the shortcut using (ShellLink shortcut = new ShellLink()) { shortcut.TargetPath = fullAppPath; shortcut.WorkingDirectory = Path.GetDirectoryName(fullAppPath); shortcut.Description = Constants.ASSEMBLY_TITLE; shortcut.WindowStyle = ShellLink.SW.SW_SHOWNORMAL; shortcut.IconPath = iconPath; shortcut.IconIndex = 0; shortcut.Save(shortcutLocation); } } }