Shortcut.cs 9.2 KB

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