comparison ShortcutKeyFinder/Shortcut.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.ComponentModel;
3 using System.Drawing;
4 using System.IO;
5
6 namespace ShortcutKeyFinder
7 {
8 /// <summary>Class to perform operations on shortcut (.lnk) files</summary>
9 class Shortcut : INotifyPropertyChanged
10 {
11 private bool _hasDuplicateHotkey;
12 private Hotkey _hotkey;
13 public event PropertyChangedEventHandler PropertyChanged;
14
15 /// <summary>Name of the shortcut as displayed by Windows Explorer</summary>
16 /// <remarks>Not necessarily the same as the shortcut's filename</remarks>
17 public string Name { get; private set; }
18
19 /// <summary>Full path of the shortcut file</summary>
20 public string Path { get; private set; }
21
22 /// <summary>Full path of the shortcut's parent directory</summary>
23 public string Location
24 {
25 get { return System.IO.Path.GetDirectoryName(Path); }
26 }
27
28 /// <summary>Display name of the shortcut's parent directory</summary>
29 /// <remarks>Gives the user a general idea of where the shortcut is located rather than the full path</remarks>
30 public string DisplayLocation
31 {
32 get {
33 return Location
34 .Replace(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Start Menu")
35 .Replace(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), "Start Menu")
36 .Replace(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Desktop")
37 .Replace(Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory), "Desktop");
38 }
39
40 }
41
42 /// <summary>Hotkey associated with the shortcut</summary>
43 public Hotkey Hotkey
44 {
45 get { return _hotkey; }
46 set
47 {
48 if (value != _hotkey)
49 {
50 _hotkey = value;
51 NotifyPropertyChanged("Hotkey");
52 }
53 }
54 }
55
56 /// <summary>Icon associated with the shortcut</summary>
57 public Image Icon { get; private set; }
58
59 /// <summary>Indicates whether this shortcut's hotkey is the same as that of another shortcut</summary>
60 /// <remarks>Always false unless set to true by some other class</remarks>
61 public bool HasDuplicateHotkey
62 {
63 get { return _hasDuplicateHotkey; }
64 set
65 {
66 if (value != _hasDuplicateHotkey)
67 {
68 _hasDuplicateHotkey = value;
69 NotifyPropertyChanged("HasDuplicateHotkey");
70 }
71 }
72 }
73
74 /// <summary>Indicates whether the user has permissions to modify the shortcut</summary>
75 /// <remarks>Always false unless set to true by some other class</remarks>
76 public bool ReadOnly { get; set; }
77
78 /// <summary>Constructor</summary>
79 /// <param name="path">Full path to a shortcut (.lnk) file</param>
80 public Shortcut(string path)
81 {
82 Path = path;
83 LoadShortcutInfo();
84 }
85
86 /// <summary>Implements the <see cref="INotifyPropertyChanged"/> interface</summary>
87 private void NotifyPropertyChanged(String info)
88 {
89 if (PropertyChanged != null)
90 {
91 PropertyChanged(this, new PropertyChangedEventArgs(info));
92 }
93 }
94
95 /// <summary>Constructor</summary>
96 private void LoadShortcutInfo()
97 {
98 ushort hotkey = Win32Helpers.GetShortcutHotkey(Path);
99 if (hotkey != 0)
100 Hotkey = new Hotkey(hotkey);
101 FileInfo info = Win32Helpers.GetFileInfo(Path, true, true);
102 if (info != null)
103 {
104 Name = info.DisplayName;
105 Icon = info.Icon.ToBitmap();
106 }
107 }
108
109 /// <summary>Shows the Windows Explorer properties window for the shortcut</summary>
110 /// <param name="parentWindow">Optional handle to a parent window for use when displaying error messages</param>
111 public void ShowExplorerPropertiesWindow(IntPtr parentWindow = default(IntPtr))
112 {
113 Win32Helpers.ShowFilePropertiesWindow(Path, parentWindow);
114 }
115
116 /// <summary>Writes the shortcut data to disk</summary>
117 public void Save()
118 {
119 if (!Win32Helpers.SetShortcutHotkey(Path, Hotkey != null ? Hotkey.RawHotkey : (ushort)0))
120 throw new UnauthorizedAccessException();
121 }
122
123 /// <summary>Determine whether two shortcuts are the same file</summary>
124 public override bool Equals(object obj)
125 {
126 Shortcut shortcut = obj as Shortcut;
127 return shortcut != null && shortcut.Path == Path;
128 }
129
130 public override int GetHashCode()
131 {
132 return Path.GetHashCode();
133 }
134 }
135 }