Shortcut.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace MediaBrowser.Controller.IO
  6. {
  7. /// <summary>
  8. /// Contains helpers to interact with shortcut files (.lnk)
  9. /// </summary>
  10. public static class Shortcut
  11. {
  12. #region Signitures were imported from http://pinvoke.net
  13. [Flags()]
  14. enum SLGP_FLAGS
  15. {
  16. /// <summary>Retrieves the standard short (8.3 format) file name</summary>
  17. SLGP_SHORTPATH = 0x1,
  18. /// <summary>Retrieves the Universal Naming Convention (UNC) path name of the file</summary>
  19. SLGP_UNCPRIORITY = 0x2,
  20. /// <summary>Retrieves the raw path name. A raw path is something that might not exist and may include environment variables that need to be expanded</summary>
  21. SLGP_RAWPATH = 0x4
  22. }
  23. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  24. struct WIN32_FIND_DATAW
  25. {
  26. public uint dwFileAttributes;
  27. public long ftCreationTime;
  28. public long ftLastAccessTime;
  29. public long ftLastWriteTime;
  30. public uint nFileSizeHigh;
  31. public uint nFileSizeLow;
  32. public uint dwReserved0;
  33. public uint dwReserved1;
  34. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  35. public string cFileName;
  36. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
  37. public string cAlternateFileName;
  38. }
  39. [Flags()]
  40. enum SLR_FLAGS
  41. {
  42. /// <summary>
  43. /// Do not display a dialog box if the link cannot be resolved. When SLR_NO_UI is set,
  44. /// the high-order word of fFlags can be set to a time-out value that specifies the
  45. /// maximum amount of time to be spent resolving the link. The function returns if the
  46. /// link cannot be resolved within the time-out duration. If the high-order word is set
  47. /// to zero, the time-out duration will be set to the default value of 3,000 milliseconds
  48. /// (3 seconds). To specify a value, set the high word of fFlags to the desired time-out
  49. /// duration, in milliseconds.
  50. /// </summary>
  51. SLR_NO_UI = 0x1,
  52. /// <summary>Obsolete and no longer used</summary>
  53. SLR_ANY_MATCH = 0x2,
  54. /// <summary>If the link object has changed, update its path and list of identifiers.
  55. /// If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine
  56. /// whether or not the link object has changed.</summary>
  57. SLR_UPDATE = 0x4,
  58. /// <summary>Do not update the link information</summary>
  59. SLR_NOUPDATE = 0x8,
  60. /// <summary>Do not execute the search heuristics</summary>
  61. SLR_NOSEARCH = 0x10,
  62. /// <summary>Do not use distributed link tracking</summary>
  63. SLR_NOTRACK = 0x20,
  64. /// <summary>Disable distributed link tracking. By default, distributed link tracking tracks
  65. /// removable media across multiple devices based on the volume name. It also uses the
  66. /// Universal Naming Convention (UNC) path to track remote file systems whose drive letter
  67. /// has changed. Setting SLR_NOLINKINFO disables both types of tracking.</summary>
  68. SLR_NOLINKINFO = 0x40,
  69. /// <summary>Call the Microsoft Windows Installer</summary>
  70. SLR_INVOKE_MSI = 0x80
  71. }
  72. /// <summary>The IShellLink interface allows Shell links to be created, modified, and resolved</summary>
  73. [ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")]
  74. interface IShellLinkW
  75. {
  76. /// <summary>Retrieves the path and file name of a Shell link object</summary>
  77. void GetPath([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATAW pfd, SLGP_FLAGS fFlags);
  78. /// <summary>Retrieves the list of item identifiers for a Shell link object</summary>
  79. void GetIDList(out IntPtr ppidl);
  80. /// <summary>Sets the pointer to an item identifier list (PIDL) for a Shell link object.</summary>
  81. void SetIDList(IntPtr pidl);
  82. /// <summary>Retrieves the description string for a Shell link object</summary>
  83. void GetDescription([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
  84. /// <summary>Sets the description for a Shell link object. The description can be any application-defined string</summary>
  85. void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
  86. /// <summary>Retrieves the name of the working directory for a Shell link object</summary>
  87. void GetWorkingDirectory([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
  88. /// <summary>Sets the name of the working directory for a Shell link object</summary>
  89. void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
  90. /// <summary>Retrieves the command-line arguments associated with a Shell link object</summary>
  91. void GetArguments([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
  92. /// <summary>Sets the command-line arguments for a Shell link object</summary>
  93. void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
  94. /// <summary>Retrieves the hot key for a Shell link object</summary>
  95. void GetHotkey(out short pwHotkey);
  96. /// <summary>Sets a hot key for a Shell link object</summary>
  97. void SetHotkey(short wHotkey);
  98. /// <summary>Retrieves the show command for a Shell link object</summary>
  99. void GetShowCmd(out int piShowCmd);
  100. /// <summary>Sets the show command for a Shell link object. The show command sets the initial show state of the window.</summary>
  101. void SetShowCmd(int iShowCmd);
  102. /// <summary>Retrieves the location (path and index) of the icon for a Shell link object</summary>
  103. void GetIconLocation([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
  104. int cchIconPath, out int piIcon);
  105. /// <summary>Sets the location (path and index) of the icon for a Shell link object</summary>
  106. void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
  107. /// <summary>Sets the relative path to the Shell link object</summary>
  108. void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
  109. /// <summary>Attempts to find the target of a Shell link, even if it has been moved or renamed</summary>
  110. void Resolve(IntPtr hwnd, SLR_FLAGS fFlags);
  111. /// <summary>Sets the path and file name of a Shell link object</summary>
  112. void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
  113. }
  114. [ComImport, Guid("0000010c-0000-0000-c000-000000000046"),
  115. InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  116. public interface IPersist
  117. {
  118. [PreserveSig]
  119. void GetClassID(out Guid pClassID);
  120. }
  121. [ComImport, Guid("0000010b-0000-0000-C000-000000000046"),
  122. InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  123. public interface IPersistFile : IPersist
  124. {
  125. new void GetClassID(out Guid pClassID);
  126. [PreserveSig]
  127. int IsDirty();
  128. [PreserveSig]
  129. void Load([In, MarshalAs(UnmanagedType.LPWStr)]
  130. string pszFileName, uint dwMode);
  131. [PreserveSig]
  132. void Save([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
  133. [In, MarshalAs(UnmanagedType.Bool)] bool remember);
  134. [PreserveSig]
  135. void SaveCompleted([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
  136. [PreserveSig]
  137. void GetCurFile([In, MarshalAs(UnmanagedType.LPWStr)] string ppszFileName);
  138. }
  139. const uint STGM_READ = 0;
  140. const int MAX_PATH = 260;
  141. // CLSID_ShellLink from ShlGuid.h
  142. [
  143. ComImport(),
  144. Guid("00021401-0000-0000-C000-000000000046")
  145. ]
  146. public class ShellLink
  147. {
  148. }
  149. #endregion
  150. public static string ResolveShortcut(string filename)
  151. {
  152. var link = new ShellLink();
  153. ((IPersistFile)link).Load(filename, STGM_READ);
  154. // TODO: if I can get hold of the hwnd call resolve first. This handles moved and renamed files.
  155. // ((IShellLinkW)link).Resolve(hwnd, 0)
  156. var sb = new StringBuilder(MAX_PATH);
  157. var data = new WIN32_FIND_DATAW();
  158. ((IShellLinkW)link).GetPath(sb, sb.Capacity, out data, 0);
  159. return sb.ToString();
  160. }
  161. public static bool IsShortcut(string filename)
  162. {
  163. return Path.GetExtension(filename).EndsWith("lnk", StringComparison.OrdinalIgnoreCase);
  164. }
  165. }
  166. }