NativeFileSystem.cs 15 KB

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