Mercurial > shortcutkeyfinder
comparison ShortcutKeyFinder/ShortcutKeyFinder.cs @ 0:209d9210c18f default tip
It works.
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Sat, 25 Jun 2016 13:42:54 +1000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:209d9210c18f |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.ComponentModel; | |
4 using System.IO; | |
5 using System.Linq; | |
6 | |
7 namespace ShortcutKeyFinder | |
8 { | |
9 /// <summary>Class to find all shortcuts that have hotkeys assigned</summary> | |
10 /// <remarks>As far as I can tell, Windows only looks for shortcut hotkeys in the Start Menu and on the Desktop. So this class does as well.</remarks> | |
11 class ShortcutKeyFinder | |
12 { | |
13 private bool _showAll; | |
14 private List<Shortcut> _allShortcuts = new List<Shortcut>(); // List to hold all the shortcuts we find, regardless of whether they have hotkeys | |
15 | |
16 /// <summary>List of all shortcuts found in the search locations</summary> | |
17 /// <remarks>If <see cref="ShowAll"/> is true, shortcuts without assigned hotkeys are included</remarks> | |
18 public BindingList<Shortcut> Shortcuts { get; private set; } | |
19 | |
20 /// <summary>Indicates whether to include shortcuts without assigned hotkeys in the list of <see cref="Shortcuts"/></summary> | |
21 public bool ShowAll | |
22 { | |
23 get { return _showAll; } | |
24 set | |
25 { | |
26 _showAll = value; | |
27 UpdateShortcutList(); | |
28 } | |
29 } | |
30 | |
31 /// <summary>Constructor</summary> | |
32 public ShortcutKeyFinder() | |
33 { | |
34 Shortcuts = new BindingList<Shortcut>(); | |
35 } | |
36 | |
37 /// <summary>Scans the search locations for shortcut files</summary> | |
38 /// <param name="Admin">Whether to consider shortcuts owned by all users as read-only</param> | |
39 public void LoadShortcuts(bool Admin = false) | |
40 { | |
41 // Reset the master list and rescan the search directories | |
42 _allShortcuts.Clear(); | |
43 LoadShortcuts(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), false); | |
44 LoadShortcuts(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), !Admin); | |
45 LoadShortcuts(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), false); | |
46 LoadShortcuts(Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory), !Admin); | |
47 // Update the public list with what we found | |
48 UpdateShortcutList(); | |
49 } | |
50 | |
51 /// <summary>Recursively scans a directory shortcut files</summary> | |
52 /// <param name="path">Directory to scan</param> | |
53 /// <param name="readOnly">Whether to consider the found shortcuts as read-only</param> | |
54 /// <remarks>The <paramref name="readOnly"/> parameter exists for speed, to avoid checking permissions on every file. Good enough for our purposes.</remarks> | |
55 private void LoadShortcuts(string path, bool readOnly) | |
56 { | |
57 try | |
58 { | |
59 foreach (string lnk in Directory.EnumerateFiles(path, "*.lnk", SearchOption.AllDirectories)) | |
60 _allShortcuts.Add(new Shortcut(lnk) { ReadOnly = readOnly }); | |
61 } | |
62 catch { } // Silently ignore access denied errors, likely nothing the user can do about it anyway | |
63 } | |
64 | |
65 /// <summary>Updates the public list of <see cref="Shortcuts"/> with the results of the most recent scan</summary> | |
66 private void UpdateShortcutList() | |
67 { | |
68 // Remove event handlers while we're manipulating the list | |
69 Shortcuts.ListChanged -= Shortcuts_ListChanged; | |
70 | |
71 // Reset the list and add all appropriate shortcuts based on the current value of ShowAll | |
72 Shortcuts.Clear(); | |
73 foreach (Shortcut shortcut in _allShortcuts) | |
74 if (shortcut.Hotkey != null || _showAll) | |
75 Shortcuts.Add(shortcut); | |
76 | |
77 // Go through the list and look for duplicates | |
78 FindDuplicateShortcuts(); | |
79 | |
80 // Re-add event handlers | |
81 Shortcuts.ListChanged += Shortcuts_ListChanged; | |
82 } | |
83 | |
84 /// <summary>Triggers a search for duplicate shortcuts when one of the shortcuts changes</summary> | |
85 void Shortcuts_ListChanged(object sender, ListChangedEventArgs e) | |
86 { | |
87 FindDuplicateShortcuts(); | |
88 } | |
89 | |
90 /// <summary>Scans for shortcuts that have duplicate hotkeys</summary> | |
91 private void FindDuplicateShortcuts() | |
92 { | |
93 Shortcuts.GroupBy(shortcut => shortcut.Hotkey).ToList().ForEach(group => group.ToList().ForEach(shortcut => shortcut.HasDuplicateHotkey = group.Count() > 1 && group.Key != null)); | |
94 } | |
95 } | |
96 } |