NativeFileSystem.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using MediaBrowser.Common.Implementations.IO;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Security;
  7. using System.Text;
  8. namespace MediaBrowser.ServerApplication.IO
  9. {
  10. public class NativeFileSystem : CommonFileSystem
  11. {
  12. public NativeFileSystem(ILogger logger)
  13. : base(logger, true)
  14. {
  15. }
  16. public override bool IsShortcut(string filename)
  17. {
  18. return base.IsShortcut(filename) ||
  19. string.Equals(Path.GetExtension(filename), ".lnk", StringComparison.OrdinalIgnoreCase);
  20. }
  21. public override string ResolveShortcut(string filename)
  22. {
  23. var path = base.ResolveShortcut(filename);
  24. if (!string.IsNullOrEmpty(path))
  25. {
  26. return path;
  27. }
  28. if (string.Equals(Path.GetExtension(filename), ".lnk", StringComparison.OrdinalIgnoreCase))
  29. {
  30. return ResolveLnk(filename);
  31. }
  32. return null;
  33. }
  34. private string ResolveLnk(string filename)
  35. {
  36. var link = new ShellLink();
  37. ((IPersistFile)link).Load(filename, NativeMethods.STGM_READ);
  38. // TODO: if I can get hold of the hwnd call resolve first. This handles moved and renamed files.
  39. // ((IShellLinkW)link).Resolve(hwnd, 0)
  40. var sb = new StringBuilder(NativeMethods.MAX_PATH);
  41. WIN32_FIND_DATA data;
  42. ((IShellLinkW)link).GetPath(sb, sb.Capacity, out data, 0);
  43. return sb.ToString();
  44. }
  45. }
  46. /// <summary>
  47. /// Class NativeMethods
  48. /// </summary>
  49. [SuppressUnmanagedCodeSecurity]
  50. public static class NativeMethods
  51. {
  52. /// <summary>
  53. /// The MA x_ PATH
  54. /// </summary>
  55. public const int MAX_PATH = 260;
  56. /// <summary>
  57. /// The MA x_ ALTERNATE
  58. /// </summary>
  59. public const int MAX_ALTERNATE = 14;
  60. /// <summary>
  61. /// The INVALI d_ HANDL e_ VALUE
  62. /// </summary>
  63. public static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
  64. /// <summary>
  65. /// The STG m_ READ
  66. /// </summary>
  67. public const uint STGM_READ = 0;
  68. }
  69. /// <summary>
  70. /// Struct FILETIME
  71. /// </summary>
  72. [StructLayout(LayoutKind.Sequential)]
  73. public struct FILETIME
  74. {
  75. /// <summary>
  76. /// The dw low date time
  77. /// </summary>
  78. public uint dwLowDateTime;
  79. /// <summary>
  80. /// The dw high date time
  81. /// </summary>
  82. public uint dwHighDateTime;
  83. }
  84. /// <summary>
  85. /// Struct WIN32_FIND_DATA
  86. /// </summary>
  87. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  88. public struct WIN32_FIND_DATA
  89. {
  90. /// <summary>
  91. /// The dw file attributes
  92. /// </summary>
  93. public FileAttributes dwFileAttributes;
  94. /// <summary>
  95. /// The ft creation time
  96. /// </summary>
  97. public FILETIME ftCreationTime;
  98. /// <summary>
  99. /// The ft last access time
  100. /// </summary>
  101. public FILETIME ftLastAccessTime;
  102. /// <summary>
  103. /// The ft last write time
  104. /// </summary>
  105. public FILETIME ftLastWriteTime;
  106. /// <summary>
  107. /// The n file size high
  108. /// </summary>
  109. public int nFileSizeHigh;
  110. /// <summary>
  111. /// The n file size low
  112. /// </summary>
  113. public int nFileSizeLow;
  114. /// <summary>
  115. /// The dw reserved0
  116. /// </summary>
  117. public int dwReserved0;
  118. /// <summary>
  119. /// The dw reserved1
  120. /// </summary>
  121. public int dwReserved1;
  122. /// <summary>
  123. /// The c file name
  124. /// </summary>
  125. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_PATH)]
  126. public string cFileName;
  127. /// <summary>
  128. /// This will always be null when FINDEX_INFO_LEVELS = basic
  129. /// </summary>
  130. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_ALTERNATE)]
  131. public string cAlternate;
  132. /// <summary>
  133. /// Gets or sets the path.
  134. /// </summary>
  135. /// <value>The path.</value>
  136. public string Path { get; set; }
  137. /// <summary>
  138. /// Returns a <see cref="System.String" /> that represents this instance.
  139. /// </summary>
  140. /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
  141. public override string ToString()
  142. {
  143. return Path ?? string.Empty;
  144. }
  145. }
  146. /// <summary>
  147. /// Enum SLGP_FLAGS
  148. /// </summary>
  149. [Flags]
  150. public enum SLGP_FLAGS
  151. {
  152. /// <summary>
  153. /// Retrieves the standard short (8.3 format) file name
  154. /// </summary>
  155. SLGP_SHORTPATH = 0x1,
  156. /// <summary>
  157. /// Retrieves the Universal Naming Convention (UNC) path name of the file
  158. /// </summary>
  159. SLGP_UNCPRIORITY = 0x2,
  160. /// <summary>
  161. /// Retrieves the raw path name. A raw path is something that might not exist and may include environment variables that need to be expanded
  162. /// </summary>
  163. SLGP_RAWPATH = 0x4
  164. }
  165. /// <summary>
  166. /// Enum SLR_FLAGS
  167. /// </summary>
  168. [Flags]
  169. public enum SLR_FLAGS
  170. {
  171. /// <summary>
  172. /// Do not display a dialog box if the link cannot be resolved. When SLR_NO_UI is set,
  173. /// the high-order word of fFlags can be set to a time-out value that specifies the
  174. /// maximum amount of time to be spent resolving the link. The function returns if the
  175. /// link cannot be resolved within the time-out duration. If the high-order word is set
  176. /// to zero, the time-out duration will be set to the default value of 3,000 milliseconds
  177. /// (3 seconds). To specify a value, set the high word of fFlags to the desired time-out
  178. /// duration, in milliseconds.
  179. /// </summary>
  180. SLR_NO_UI = 0x1,
  181. /// <summary>
  182. /// Obsolete and no longer used
  183. /// </summary>
  184. SLR_ANY_MATCH = 0x2,
  185. /// <summary>
  186. /// If the link object has changed, update its path and list of identifiers.
  187. /// If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine
  188. /// whether or not the link object has changed.
  189. /// </summary>
  190. SLR_UPDATE = 0x4,
  191. /// <summary>
  192. /// Do not update the link information
  193. /// </summary>
  194. SLR_NOUPDATE = 0x8,
  195. /// <summary>
  196. /// Do not execute the search heuristics
  197. /// </summary>
  198. SLR_NOSEARCH = 0x10,
  199. /// <summary>
  200. /// Do not use distributed link tracking
  201. /// </summary>
  202. SLR_NOTRACK = 0x20,
  203. /// <summary>
  204. /// Disable distributed link tracking. By default, distributed link tracking tracks
  205. /// removable media across multiple devices based on the volume name. It also uses the
  206. /// Universal Naming Convention (UNC) path to track remote file systems whose drive letter
  207. /// has changed. Setting SLR_NOLINKINFO disables both types of tracking.
  208. /// </summary>
  209. SLR_NOLINKINFO = 0x40,
  210. /// <summary>
  211. /// Call the Microsoft Windows Installer
  212. /// </summary>
  213. SLR_INVOKE_MSI = 0x80
  214. }
  215. /// <summary>
  216. /// The IShellLink interface allows Shell links to be created, modified, and resolved
  217. /// </summary>
  218. [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")]
  219. public interface IShellLinkW
  220. {
  221. /// <summary>
  222. /// Retrieves the path and file name of a Shell link object
  223. /// </summary>
  224. /// <param name="pszFile">The PSZ file.</param>
  225. /// <param name="cchMaxPath">The CCH max path.</param>
  226. /// <param name="pfd">The PFD.</param>
  227. /// <param name="fFlags">The f flags.</param>
  228. void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATA pfd, SLGP_FLAGS fFlags);
  229. /// <summary>
  230. /// Retrieves the list of item identifiers for a Shell link object
  231. /// </summary>
  232. /// <param name="ppidl">The ppidl.</param>
  233. void GetIDList(out IntPtr ppidl);
  234. /// <summary>
  235. /// Sets the pointer to an item identifier list (PIDL) for a Shell link object.
  236. /// </summary>
  237. /// <param name="pidl">The pidl.</param>
  238. void SetIDList(IntPtr pidl);
  239. /// <summary>
  240. /// Retrieves the description string for a Shell link object
  241. /// </summary>
  242. /// <param name="pszName">Name of the PSZ.</param>
  243. /// <param name="cchMaxName">Name of the CCH max.</param>
  244. void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
  245. /// <summary>
  246. /// Sets the description for a Shell link object. The description can be any application-defined string
  247. /// </summary>
  248. /// <param name="pszName">Name of the PSZ.</param>
  249. void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
  250. /// <summary>
  251. /// Retrieves the name of the working directory for a Shell link object
  252. /// </summary>
  253. /// <param name="pszDir">The PSZ dir.</param>
  254. /// <param name="cchMaxPath">The CCH max path.</param>
  255. void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
  256. /// <summary>
  257. /// Sets the name of the working directory for a Shell link object
  258. /// </summary>
  259. /// <param name="pszDir">The PSZ dir.</param>
  260. void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
  261. /// <summary>
  262. /// Retrieves the command-line arguments associated with a Shell link object
  263. /// </summary>
  264. /// <param name="pszArgs">The PSZ args.</param>
  265. /// <param name="cchMaxPath">The CCH max path.</param>
  266. void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
  267. /// <summary>
  268. /// Sets the command-line arguments for a Shell link object
  269. /// </summary>
  270. /// <param name="pszArgs">The PSZ args.</param>
  271. void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
  272. /// <summary>
  273. /// Retrieves the hot key for a Shell link object
  274. /// </summary>
  275. /// <param name="pwHotkey">The pw hotkey.</param>
  276. void GetHotkey(out short pwHotkey);
  277. /// <summary>
  278. /// Sets a hot key for a Shell link object
  279. /// </summary>
  280. /// <param name="wHotkey">The w hotkey.</param>
  281. void SetHotkey(short wHotkey);
  282. /// <summary>
  283. /// Retrieves the show command for a Shell link object
  284. /// </summary>
  285. /// <param name="piShowCmd">The pi show CMD.</param>
  286. void GetShowCmd(out int piShowCmd);
  287. /// <summary>
  288. /// Sets the show command for a Shell link object. The show command sets the initial show state of the window.
  289. /// </summary>
  290. /// <param name="iShowCmd">The i show CMD.</param>
  291. void SetShowCmd(int iShowCmd);
  292. /// <summary>
  293. /// Retrieves the location (path and index) of the icon for a Shell link object
  294. /// </summary>
  295. /// <param name="pszIconPath">The PSZ icon path.</param>
  296. /// <param name="cchIconPath">The CCH icon path.</param>
  297. /// <param name="piIcon">The pi icon.</param>
  298. void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
  299. int cchIconPath, out int piIcon);
  300. /// <summary>
  301. /// Sets the location (path and index) of the icon for a Shell link object
  302. /// </summary>
  303. /// <param name="pszIconPath">The PSZ icon path.</param>
  304. /// <param name="iIcon">The i icon.</param>
  305. void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
  306. /// <summary>
  307. /// Sets the relative path to the Shell link object
  308. /// </summary>
  309. /// <param name="pszPathRel">The PSZ path rel.</param>
  310. /// <param name="dwReserved">The dw reserved.</param>
  311. void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
  312. /// <summary>
  313. /// Attempts to find the target of a Shell link, even if it has been moved or renamed
  314. /// </summary>
  315. /// <param name="hwnd">The HWND.</param>
  316. /// <param name="fFlags">The f flags.</param>
  317. void Resolve(IntPtr hwnd, SLR_FLAGS fFlags);
  318. /// <summary>
  319. /// Sets the path and file name of a Shell link object
  320. /// </summary>
  321. /// <param name="pszFile">The PSZ file.</param>
  322. void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
  323. }
  324. /// <summary>
  325. /// Interface IPersist
  326. /// </summary>
  327. [ComImport, Guid("0000010c-0000-0000-c000-000000000046"),
  328. InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  329. public interface IPersist
  330. {
  331. /// <summary>
  332. /// Gets the class ID.
  333. /// </summary>
  334. /// <param name="pClassID">The p class ID.</param>
  335. [PreserveSig]
  336. void GetClassID(out Guid pClassID);
  337. }
  338. /// <summary>
  339. /// Interface IPersistFile
  340. /// </summary>
  341. [ComImport, Guid("0000010b-0000-0000-C000-000000000046"),
  342. InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  343. public interface IPersistFile : IPersist
  344. {
  345. /// <summary>
  346. /// Gets the class ID.
  347. /// </summary>
  348. /// <param name="pClassID">The p class ID.</param>
  349. new void GetClassID(out Guid pClassID);
  350. /// <summary>
  351. /// Determines whether this instance is dirty.
  352. /// </summary>
  353. [PreserveSig]
  354. int IsDirty();
  355. /// <summary>
  356. /// Loads the specified PSZ file name.
  357. /// </summary>
  358. /// <param name="pszFileName">Name of the PSZ file.</param>
  359. /// <param name="dwMode">The dw mode.</param>
  360. [PreserveSig]
  361. void Load([In, MarshalAs(UnmanagedType.LPWStr)]
  362. string pszFileName, uint dwMode);
  363. /// <summary>
  364. /// Saves the specified PSZ file name.
  365. /// </summary>
  366. /// <param name="pszFileName">Name of the PSZ file.</param>
  367. /// <param name="remember">if set to <c>true</c> [remember].</param>
  368. [PreserveSig]
  369. void Save([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
  370. [In, MarshalAs(UnmanagedType.Bool)] bool remember);
  371. /// <summary>
  372. /// Saves the completed.
  373. /// </summary>
  374. /// <param name="pszFileName">Name of the PSZ file.</param>
  375. [PreserveSig]
  376. void SaveCompleted([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
  377. /// <summary>
  378. /// Gets the cur file.
  379. /// </summary>
  380. /// <param name="ppszFileName">Name of the PPSZ file.</param>
  381. [PreserveSig]
  382. void GetCurFile([In, MarshalAs(UnmanagedType.LPWStr)] string ppszFileName);
  383. }
  384. // CLSID_ShellLink from ShlGuid.h
  385. /// <summary>
  386. /// Class ShellLink
  387. /// </summary>
  388. [
  389. ComImport,
  390. Guid("00021401-0000-0000-C000-000000000046")
  391. ]
  392. public class ShellLink
  393. {
  394. }
  395. }