Mercurial > shortcutkeyfinder
view 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 |
line wrap: on
line source
using System; using System.ComponentModel; using System.Drawing; using System.IO; namespace ShortcutKeyFinder { /// <summary>Class to perform operations on shortcut (.lnk) files</summary> class Shortcut : INotifyPropertyChanged { private bool _hasDuplicateHotkey; private Hotkey _hotkey; public event PropertyChangedEventHandler PropertyChanged; /// <summary>Name of the shortcut as displayed by Windows Explorer</summary> /// <remarks>Not necessarily the same as the shortcut's filename</remarks> public string Name { get; private set; } /// <summary>Full path of the shortcut file</summary> public string Path { get; private set; } /// <summary>Full path of the shortcut's parent directory</summary> public string Location { get { return System.IO.Path.GetDirectoryName(Path); } } /// <summary>Display name of the shortcut's parent directory</summary> /// <remarks>Gives the user a general idea of where the shortcut is located rather than the full path</remarks> public string DisplayLocation { get { return Location .Replace(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Start Menu") .Replace(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), "Start Menu") .Replace(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Desktop") .Replace(Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory), "Desktop"); } } /// <summary>Hotkey associated with the shortcut</summary> public Hotkey Hotkey { get { return _hotkey; } set { if (value != _hotkey) { _hotkey = value; NotifyPropertyChanged("Hotkey"); } } } /// <summary>Icon associated with the shortcut</summary> public Image Icon { get; private set; } /// <summary>Indicates whether this shortcut's hotkey is the same as that of another shortcut</summary> /// <remarks>Always false unless set to true by some other class</remarks> public bool HasDuplicateHotkey { get { return _hasDuplicateHotkey; } set { if (value != _hasDuplicateHotkey) { _hasDuplicateHotkey = value; NotifyPropertyChanged("HasDuplicateHotkey"); } } } /// <summary>Indicates whether the user has permissions to modify the shortcut</summary> /// <remarks>Always false unless set to true by some other class</remarks> public bool ReadOnly { get; set; } /// <summary>Constructor</summary> /// <param name="path">Full path to a shortcut (.lnk) file</param> public Shortcut(string path) { Path = path; LoadShortcutInfo(); } /// <summary>Implements the <see cref="INotifyPropertyChanged"/> interface</summary> private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } /// <summary>Constructor</summary> private void LoadShortcutInfo() { ushort hotkey = Win32Helpers.GetShortcutHotkey(Path); if (hotkey != 0) Hotkey = new Hotkey(hotkey); FileInfo info = Win32Helpers.GetFileInfo(Path, true, true); if (info != null) { Name = info.DisplayName; Icon = info.Icon.ToBitmap(); } } /// <summary>Shows the Windows Explorer properties window for the shortcut</summary> /// <param name="parentWindow">Optional handle to a parent window for use when displaying error messages</param> public void ShowExplorerPropertiesWindow(IntPtr parentWindow = default(IntPtr)) { Win32Helpers.ShowFilePropertiesWindow(Path, parentWindow); } /// <summary>Writes the shortcut data to disk</summary> public void Save() { if (!Win32Helpers.SetShortcutHotkey(Path, Hotkey != null ? Hotkey.RawHotkey : (ushort)0)) throw new UnauthorizedAccessException(); } /// <summary>Determine whether two shortcuts are the same file</summary> public override bool Equals(object obj) { Shortcut shortcut = obj as Shortcut; return shortcut != null && shortcut.Path == Path; } public override int GetHashCode() { return Path.GetHashCode(); } } }