public static LnkData ResolveShortcut(string lnkFileName, string pathtoLnk, string _iconFilePath, int _iconIndex) { WshShell shell = new WshShell(); LnkData lnkData = null; try { string shortcutAddress = null; if (pathtoLnk != null) { shortcutAddress = System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(pathtoLnk)); } if (null == shortcutAddress || string.Empty == shortcutAddress) { object shDesktop = (object)"Desktop"; shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop); } else { shortcutAddress = pathtoLnk; } shortcutAddress += @"\" + lnkFileName; bool bTisD = (System.IO.File.GetAttributes(shortcutAddress).HasFlag(FileAttributes.Directory)); if (bTisD || System.IO.File.Exists(shortcutAddress)) { // the link itself is a desktop directory and not a link if (bTisD) { lnkData = new LnkData { Arguments = string.Empty, Description = "Folder", FullName = System.IO.Path.GetDirectoryName(shortcutAddress), Hotkey = string.Empty, TargetPath = shortcutAddress, OriginalTargetPath = shortcutAddress, //WindowStyle = 1, // 1 for default window, 3 for maximize, 7 for minimize should remap to 1 = ProcessWindowStyle.Normal //WorkingDirectory = string.Empty, bIsReference = false, bTargetIsDirectory = true, ShortcutAddress = shortcutAddress, IconSourceFilePath = _iconFilePath, IconIndex = _iconIndex, icoBitmap = ico2bmap.ExtractICO(_iconFilePath, _iconIndex) }; } else // if this is a reference or a link or a file or directory { bool bIsRef = (".appref-ms" == System.IO.Path.GetExtension(shortcutAddress).ToLower()); // if this is a reference use the reference as the target if (bIsRef) { lnkData = new LnkData { Arguments = string.Empty, Description = System.IO.Path.GetFileNameWithoutExtension(shortcutAddress), FullName = System.IO.Path.GetFileName(shortcutAddress), Hotkey = string.Empty, TargetPath = shortcutAddress, OriginalTargetPath = shortcutAddress, //WindowStyle = 1, // 1 for default window, 3 for maximize, 7 for minimize should remap to 1 = ProcessWindowStyle.Normal //WorkingDirectory = string.Empty, bIsReference = bIsRef, bTargetIsDirectory = false, ShortcutAddress = shortcutAddress, //IconSourceFilePath = string.Empty, IconSourceFilePath = _iconFilePath, IconIndex = _iconIndex, icoBitmap = ico2bmap.ExtractICO(_iconFilePath, _iconIndex) }; } else if (".lnk" == System.IO.Path.GetExtension(shortcutAddress).ToLower()) { //IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); //if (string.IsNullOrEmpty(shortcut.TargetPath)) //{ //MessageBox.Show("The LNK file you attempted to import has no valid target data. Edit the shortcut. Note: if this" + // " is a 'This PC' shortcut, this is not possible.", "LNK Import Error", // MessageBoxButton.OK, MessageBoxImage.Error); //string s = ReadLnk(shortcutAddress); //throw new Exception("Shortcut does not have a standard target"); // Make sure icon source actually exists. If not use the LNK file for icon if (!(System.IO.File.Exists(_iconFilePath) || System.IO.Directory.Exists(_iconFilePath))) { _iconFilePath = shortcutAddress; _iconIndex = ico2bmap.USE_MAIN_ICON; } // Just run the lnk file // do not need to have knowledge of what to run just make file the target // but do get the target file so the original target icon can be selected by the user lnkData = new LnkData { Arguments = string.Empty, Description = System.IO.Path.GetFileNameWithoutExtension(shortcutAddress) + " Link", FullName = System.IO.Path.GetFileName(shortcutAddress), Hotkey = string.Empty, TargetPath = shortcutAddress, OriginalTargetPath = ReadLnk(shortcutAddress), //WindowStyle = 1, // 1 for default window, 3 for maximize, 7 for minimize should remap to 1 = ProcessWindowStyle.Normal //WorkingDirectory = string.Empty, bIsReference = false, bTargetIsDirectory = false, ShortcutAddress = shortcutAddress, bTargetIsFile = true, IconSourceFilePath = _iconFilePath, IconIndex = _iconIndex, icoBitmap = ico2bmap.ExtractICO(_iconFilePath, _iconIndex) }; } else // this is not a link it's a file - get type and set target to assigned app { string ext = System.IO.Path.GetExtension(shortcutAddress).ToLower(); // do not need to have knowledge of what to run just make file the target lnkData = new LnkData { Arguments = string.Empty, Description = ext.ToUpper() + " File", FullName = System.IO.Path.GetFileName(shortcutAddress), Hotkey = string.Empty, TargetPath = shortcutAddress, OriginalTargetPath = shortcutAddress, //WindowStyle = 1, // 1 for default window, 3 for maximize, 7 for minimize should remap to 1 = ProcessWindowStyle.Normal //WorkingDirectory = string.Empty, bIsReference = false, bTargetIsDirectory = false, ShortcutAddress = shortcutAddress, bTargetIsFile = true, IconSourceFilePath = _iconFilePath, IconIndex = _iconIndex, icoBitmap = ico2bmap.ExtractICO(_iconFilePath, _iconIndex) }; } } } } catch (Exception e) { WriteProgramLog(e.Message + " Did not resolve shortcut: " + lnkFileName); lnkData = null; } return(lnkData); }
/// <summary> /// Create the KickOff link /// </summary> /// <param name="FileList"></param> private void CreateShortCuts(string[] FileList) { foreach (string shortcutini in FileList) { if (null == shortcutini || string.Empty == shortcutini) { continue; } /// Check if a sub dialog is being defined /// If so create a new dialog /// /// The shortcut file names may be a path to a link file or target file/directory /// or it may be that and a user selected icon source and index separated using /// a split character string iconFilePath = string.Empty; int iconIndex = USE_MAIN_ICON; string FilePath = string.Empty; string[] sc_args = null; try { sc_args = shortcutini.Split(INI_SPLIT_CHAR); FilePath = Environment.ExpandEnvironmentVariables(sc_args[0]); if (sc_args.Length == 3) { iconFilePath = Environment.ExpandEnvironmentVariables(sc_args[1]); iconIndex = int.Parse(sc_args[2]); } else if (sc_args.Length != 1) { WriteProgramLog("Kickoff.ini read error. Shortcut dataset should be 1 or 3 items and is " + sc_args.Length.ToString()); } else { iconFilePath = sc_args[0]; // set icon path to the link file } } catch (Exception e) { WriteProgramLog(FilePath + " Error: " + e.Message); // screwed up so move on continue; } // expand the paths //FilePath = Environment.ExpandEnvironmentVariables(FilePath); //iconFilePath = Environment.ExpandEnvironmentVariables(iconFilePath); /// Test here that FilePath, iconFilePath, iconIndex have sane values and point to something /// if (!System.IO.File.Exists(FilePath)) { if (!System.IO.Directory.Exists(FilePath)) { //System.IO.File.GetAttributes(FilePath).HasFlag(FileAttributes.Directory); WriteProgramLog(FilePath + ": Windows thinks this is not a file or directory path.\n\tCould be a permissions/security issue." ); // screwed up move on continue; } } // path to icons could be a direstory if the target is a directory if (!System.IO.File.Exists(iconFilePath)) { if (!System.IO.Directory.Exists(iconFilePath)) { WriteProgramLog(iconFilePath + ": Windows thinks this is not a file or directory path.\n\tCould be an invalid path or permissions/security issue." ); // reassign the icon source iconFilePath = FilePath; } } //if (iconIndex == 0) iconIndex = USE_MAIN_ICON; //else if (iconIndex < USE_MAIN_ICON || iconIndex > ico2bmap.MAX_ICONS) { WriteProgramLog("Icon index: " + sc_args[2].ToString() + " is an invalid value"); iconIndex = USE_MAIN_ICON; } LnkData lnk = lnkio.ResolveShortcut(FilePath, iconFilePath, iconIndex); if (null != lnk) { if (null == lnk || lnk.ShortcutAddress == string.Empty) { WriteProgramLog("Shortcut link does not have a path:" + FilePath); //throw new Exception("Shortcut link does not have a path"); } Shortcut sc = new Shortcut { lnkData = lnk, lnkPopup = new Popup(), IsRendered = false }; var items = Shortcuts.Where(x => x.lnkData.ShortcutAddress == FilePath); if (items.Count() == 0) // the link imported is not in the existing list so add it { /// Add the shortcut to the list Shortcuts.Add(sc); } } } }
public static LnkData ResolveShortcut(string lnkFileName, string pathtoLnk, string _iconFilePath, int _iconIndex) { WshShell shell = new WshShell(); LnkData lnkData = null; try { string shortcutAddress = null; if (pathtoLnk != null) shortcutAddress = System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(pathtoLnk)); if (null == shortcutAddress || string.Empty == shortcutAddress) { object shDesktop = (object)"Desktop"; shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop); } else { shortcutAddress = pathtoLnk; } shortcutAddress += @"\" + lnkFileName; bool bTisD = (System.IO.File.GetAttributes(shortcutAddress).HasFlag(FileAttributes.Directory)); if (bTisD || System.IO.File.Exists(shortcutAddress)) { // the link itself is a desktop directory and not a link if (bTisD) { lnkData = new LnkData { Arguments = string.Empty, Description = "Folder", FullName = System.IO.Path.GetDirectoryName(shortcutAddress), Hotkey = string.Empty, TargetPath = shortcutAddress, OriginalTargetPath = shortcutAddress, //WindowStyle = 1, // 1 for default window, 3 for maximize, 7 for minimize should remap to 1 = ProcessWindowStyle.Normal //WorkingDirectory = string.Empty, bIsReference = false, bTargetIsDirectory = true, ShortcutAddress = shortcutAddress, IconSourceFilePath = _iconFilePath, IconIndex = _iconIndex, icoBitmap = ico2bmap.ExtractICO(_iconFilePath, _iconIndex) }; } else // if this is a reference or a link or a file or directory { bool bIsRef = (".appref-ms" == System.IO.Path.GetExtension(shortcutAddress).ToLower()); // if this is a reference use the reference as the target if (bIsRef) { lnkData = new LnkData { Arguments = string.Empty, Description = System.IO.Path.GetFileNameWithoutExtension(shortcutAddress), FullName = System.IO.Path.GetFileName(shortcutAddress), Hotkey = string.Empty, TargetPath = shortcutAddress, OriginalTargetPath = shortcutAddress, //WindowStyle = 1, // 1 for default window, 3 for maximize, 7 for minimize should remap to 1 = ProcessWindowStyle.Normal //WorkingDirectory = string.Empty, bIsReference = bIsRef, bTargetIsDirectory = false, ShortcutAddress = shortcutAddress, //IconSourceFilePath = string.Empty, IconSourceFilePath = _iconFilePath, IconIndex = _iconIndex, icoBitmap = ico2bmap.ExtractICO(_iconFilePath, _iconIndex) }; } else if (".lnk" == System.IO.Path.GetExtension(shortcutAddress).ToLower()) { //IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); //if (string.IsNullOrEmpty(shortcut.TargetPath)) //{ //MessageBox.Show("The LNK file you attempted to import has no valid target data. Edit the shortcut. Note: if this" + // " is a 'This PC' shortcut, this is not possible.", "LNK Import Error", // MessageBoxButton.OK, MessageBoxImage.Error); //string s = ReadLnk(shortcutAddress); //throw new Exception("Shortcut does not have a standard target"); // Make sure icon source actually exists. If not use the LNK file for icon if (!(System.IO.File.Exists(_iconFilePath) ||System.IO.Directory.Exists(_iconFilePath))) { _iconFilePath = shortcutAddress; _iconIndex = ico2bmap.USE_MAIN_ICON; } // Just run the lnk file // do not need to have knowledge of what to run just make file the target // but do get the taget file so the original target icon can be selected by the user lnkData = new LnkData { Arguments = string.Empty, Description = System.IO.Path.GetFileNameWithoutExtension(shortcutAddress) + " Link", FullName = System.IO.Path.GetFileName(shortcutAddress), Hotkey = string.Empty, TargetPath = shortcutAddress, OriginalTargetPath = ReadLnk(shortcutAddress), //WindowStyle = 1, // 1 for default window, 3 for maximize, 7 for minimize should remap to 1 = ProcessWindowStyle.Normal //WorkingDirectory = string.Empty, bIsReference = false, bTargetIsDirectory = false, ShortcutAddress = shortcutAddress, bTargetIsFile = true, IconSourceFilePath = _iconFilePath, IconIndex = _iconIndex, icoBitmap = ico2bmap.ExtractICO(_iconFilePath, _iconIndex) }; } else // this is not a link it's a file - get type and set target to assigned app { string ext = System.IO.Path.GetExtension(shortcutAddress).ToLower(); // do not need to have knowledge of what to run just make file the target lnkData = new LnkData { Arguments = string.Empty, Description = ext.ToUpper() + " File", FullName = System.IO.Path.GetFileName(shortcutAddress), Hotkey = string.Empty, TargetPath = shortcutAddress, OriginalTargetPath = shortcutAddress, //WindowStyle = 1, // 1 for default window, 3 for maximize, 7 for minimize should remap to 1 = ProcessWindowStyle.Normal //WorkingDirectory = string.Empty, bIsReference = false, bTargetIsDirectory = false, ShortcutAddress = shortcutAddress, bTargetIsFile = true, IconSourceFilePath = _iconFilePath, IconIndex = _iconIndex, icoBitmap = ico2bmap.ExtractICO(_iconFilePath, _iconIndex) }; } } } } catch (Exception e) { WriteProgramLog(e.Message + " Did not resolve shortcut: " + lnkFileName); lnkData = null; } return (lnkData); }