/// <summary> /// Gather a list of ShortcutItems from the current environment /// </summary> /// <returns></returns> public static List <ShortcutItem> GetShortcuts(bool refreshCache = false) { if (refreshCache) { _shortcutsCache = null; } if (_shortcutsCache != null) { return(_shortcutsCache); } var shortcutsList = new List <ShortcutItem>(); var pathsToScan = new List <string> { @"%PROGRAMDATA%\Microsoft\Windows\Start Menu", @"%APPDATA%\Microsoft\Windows\Start Menu" }; foreach (var pathToScan in pathsToScan) { //Recursively go through all folders of lnk files, adding each ShortcutItem to the list Action <string, Action <string> > applyAllFiles = null; applyAllFiles = (folder, fileAction) => { foreach (var file in Directory.GetFiles(folder)) { fileAction(file); } foreach (var subDir in Directory.GetDirectories(folder)) { try { applyAllFiles(subDir, fileAction); } catch { // ignored } } }; applyAllFiles(Environment.ExpandEnvironmentVariables(pathToScan), f => { var extension = Path.GetExtension(f); if (extension != null && !extension.Equals(".lnk", StringComparison.OrdinalIgnoreCase)) { return; } var shortcutItem = new ShortcutItem(f); if (shortcutItem.IsValidForIconification) { shortcutsList.Add(shortcutItem); } }); } //Order the list by name _shortcutsCache = shortcutsList.OrderBy(f => f.ShortcutFileInfo.Name).ToList(); return(_shortcutsCache); }
/// <summary> /// Gather a list of ShortcutItems from the current environment /// </summary> /// <returns></returns> public static List <ShortcutItem> GetShortcuts(bool refreshCache = false) { if (refreshCache) { _shortcutsCache = null; } if (_shortcutsCache != null) { return(_shortcutsCache); } var shortcutsList = new List <ShortcutItem>(); //use all possible sources of start menu items var pathsToScan = new List <string> { Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) }; foreach (var pathToScan in pathsToScan) { //Recursively go through all folders of lnk files, adding each ShortcutItem to the list Action <string, Action <string> > applyAllFiles = null; applyAllFiles = (folder, fileAction) => { foreach (var file in Directory.GetFiles(folder)) { fileAction(file); } foreach (var subDir in Directory.GetDirectories(folder)) { try { applyAllFiles(subDir, fileAction); } catch { // ignored } } }; //potentially a start menu path may have resolved incorrectly if a user has tinkered? if (!Directory.Exists(pathToScan)) { continue; } applyAllFiles(pathToScan, f => { var extension = Path.GetExtension(f); if (extension != null && !extension.Equals(".lnk", StringComparison.OrdinalIgnoreCase)) { return; } var shortcutItem = new ShortcutItem(f); if (shortcutItem.IsValidForIconification) { shortcutsList.Add(shortcutItem); } }); } //Order the list by name _shortcutsCache = shortcutsList.OrderBy(f => f.ShortcutFileInfo.Name).ToList(); return(_shortcutsCache); }