You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

843 lines
43 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.Diagnostics.CodeAnalysis;
  23. using System.Runtime.InteropServices;
  24. using System.Security;
  25. using System.Text;
  26. namespace Alphaleonis.Win32.Filesystem
  27. {
  28. /// <summary>Provides access to a file system object, using Shell32.</summary>
  29. public static class Shell32
  30. {
  31. /// <summary>Provides information for the IQueryAssociations interface methods, used by Shell32.</summary>
  32. [Flags]
  33. public enum AssociationAttributes
  34. {
  35. /// <summary>None.</summary>
  36. None = 0,
  37. /// <summary>Instructs not to map CLSID values to ProgID values.</summary>
  38. InitNoRemapClsid = 1,
  39. /// <summary>Identifies the value of the supplied file parameter (3rd parameter of function GetFileAssociation()) as an executable file name.</summary>
  40. /// <remarks>If this flag is not set, the root key will be set to the ProgID associated with the .exe key instead of the executable file's ProgID.</remarks>
  41. InitByExeName = 2,
  42. /// <summary>Specifies that when an IQueryAssociation method does not find the requested value under the root key, it should attempt to retrieve the comparable value from the * subkey.</summary>
  43. InitDefaultToStar = 4,
  44. /// <summary>Specifies that when an IQueryAssociation method does not find the requested value under the root key, it should attempt to retrieve the comparable value from the Folder subkey.</summary>
  45. InitDefaultToFolder = 8,
  46. /// <summary>Specifies that only HKEY_CLASSES_ROOT should be searched, and that HKEY_CURRENT_USER should be ignored.</summary>
  47. NoUserSettings = 16,
  48. /// <summary>Specifies that the return string should not be truncated. Instead, return an error value and the required size for the complete string.</summary>
  49. NoTruncate = 32,
  50. /// <summary>
  51. /// Instructs IQueryAssociations methods to verify that data is accurate.
  52. /// This setting allows IQueryAssociations methods to read data from the user's hard disk for verification.
  53. /// For example, they can check the friendly name in the registry against the one stored in the .exe file.
  54. /// </summary>
  55. /// <remarks>Setting this flag typically reduces the efficiency of the method.</remarks>
  56. Verify = 64,
  57. /// <summary>
  58. /// Instructs IQueryAssociations methods to ignore Rundll.exe and return information about its target.
  59. /// Typically IQueryAssociations methods return information about the first .exe or .dll in a command string.
  60. /// If a command uses Rundll.exe, setting this flag tells the method to ignore Rundll.exe and return information about its target.
  61. /// </summary>
  62. RemapRunDll = 128,
  63. /// <summary>Instructs IQueryAssociations methods not to fix errors in the registry, such as the friendly name of a function not matching the one found in the .exe file.</summary>
  64. [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "FixUps")]
  65. NoFixUps = 256,
  66. /// <summary>Specifies that the BaseClass value should be ignored.</summary>
  67. IgnoreBaseClass = 512,
  68. /// <summary>Specifies that the "Unknown" ProgID should be ignored; instead, fail.</summary>
  69. /// <remarks>Introduced in Windows 7.</remarks>
  70. InitIgnoreUnknown = 1024,
  71. /// <summary>Specifies that the supplied ProgID should be mapped using the system defaults, rather than the current user defaults.</summary>
  72. /// <remarks>Introduced in Windows 8.</remarks>
  73. InitFixedProgId = 2048,
  74. /// <summary>Specifies that the value is a protocol, and should be mapped using the current user defaults.</summary>
  75. /// <remarks>Introduced in Windows 8.</remarks>
  76. IsProtocol = 4096
  77. }
  78. //internal enum AssociationData
  79. //{
  80. // MsiDescriptor = 1,
  81. // NoActivateHandler = 2 ,
  82. // QueryClassStore = 3,
  83. // HasPerUserAssoc = 4,
  84. // EditFlags = 5,
  85. // Value = 6
  86. //}
  87. //internal enum AssociationKey
  88. //{
  89. // ShellExecClass = 1,
  90. // App = 2,
  91. // Class = 3,
  92. // BaseClass = 4
  93. //}
  94. /// <summary>ASSOCSTR enumeration - Used by the AssocQueryString() function to define the type of string that is to be returned.</summary>
  95. public enum AssociationString
  96. {
  97. /// <summary>None.</summary>
  98. None = 0,
  99. /// <summary>A command string associated with a Shell verb.</summary>
  100. Command = 1,
  101. /// <summary>
  102. /// An executable from a Shell verb command string.
  103. /// For example, this string is found as the (Default) value for a subkey such as HKEY_CLASSES_ROOT\ApplicationName\shell\Open\command.
  104. /// If the command uses Rundll.exe, set the <see cref="AssociationAttributes.RemapRunDll"/> flag in the attributes parameter of IQueryAssociations::GetString to retrieve the target executable.
  105. /// </summary>
  106. Executable = 2,
  107. /// <summary>The friendly name of a document type.</summary>
  108. FriendlyDocName = 3,
  109. /// <summary>The friendly name of an executable file.</summary>
  110. FriendlyAppName = 4,
  111. /// <summary>Ignore the information associated with the open subkey.</summary>
  112. NoOpen = 5,
  113. /// <summary>Look under the ShellNew subkey.</summary>
  114. ShellNewValue = 6,
  115. /// <summary>A template for DDE commands.</summary>
  116. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Dde")]
  117. DdeCommand = 7,
  118. /// <summary>The DDE command to use to create a process.</summary>
  119. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Dde")]
  120. DdeIfExec = 8,
  121. /// <summary>The application name in a DDE broadcast.</summary>
  122. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Dde")]
  123. DdeApplication = 9,
  124. /// <summary>The topic name in a DDE broadcast.</summary>
  125. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Dde")]
  126. DdeTopic = 10,
  127. /// <summary>
  128. /// Corresponds to the InfoTip registry value.
  129. /// Returns an info tip for an item, or list of properties in the form of an IPropertyDescriptionList from which to create an info tip, such as when hovering the cursor over a file name.
  130. /// The list of properties can be parsed with PSGetPropertyDescriptionListFromString.
  131. /// </summary>
  132. InfoTip = 11,
  133. /// <summary>
  134. /// Corresponds to the QuickTip registry value. This is the same as <see cref="InfoTip"/>, except that it always returns a list of property names in the form of an IPropertyDescriptionList.
  135. /// The difference between this value and <see cref="InfoTip"/> is that this returns properties that are safe for any scenario that causes slow property retrieval, such as offline or slow networks.
  136. /// Some of the properties returned from <see cref="InfoTip"/> might not be appropriate for slow property retrieval scenarios.
  137. /// The list of properties can be parsed with PSGetPropertyDescriptionListFromString.
  138. /// </summary>
  139. QuickTip = 12,
  140. /// <summary>
  141. /// Corresponds to the TileInfo registry value. Contains a list of properties to be displayed for a particular file type in a Windows Explorer window that is in tile view.
  142. /// This is the same as <see cref="InfoTip"/>, but, like <see cref="QuickTip"/>, it also returns a list of property names in the form of an IPropertyDescriptionList.
  143. /// The list of properties can be parsed with PSGetPropertyDescriptionListFromString.
  144. /// </summary>
  145. TileInfo = 13,
  146. /// <summary>
  147. /// Describes a general type of MIME file association, such as image and bmp,
  148. /// so that applications can make general assumptions about a specific file type.
  149. /// </summary>
  150. ContentType = 14,
  151. /// <summary>
  152. /// Returns the path to the icon resources to use by default for this association.
  153. /// Positive numbers indicate an index into the dll's resource table, while negative numbers indicate a resource ID.
  154. /// An example of the syntax for the resource is "c:\myfolder\myfile.dll,-1".
  155. /// </summary>
  156. DefaultIcon = 15,
  157. /// <summary>
  158. /// For an object that has a Shell extension associated with it,
  159. /// you can use this to retrieve the CLSID of that Shell extension object by passing a string representation
  160. /// of the IID of the interface you want to retrieve as the pwszExtra parameter of IQueryAssociations::GetString.
  161. /// For example, if you want to retrieve a handler that implements the IExtractImage interface,
  162. /// you would specify "{BB2E617C-0920-11d1-9A0B-00C04FC2D6C1}", which is the IID of IExtractImage.
  163. /// </summary>
  164. ShellExtension = 16,
  165. /// <summary>
  166. /// For a verb invoked through COM and the IDropTarget interface, you can use this flag to retrieve the IDropTarget object's CLSID.
  167. /// This CLSID is registered in the DropTarget subkey.
  168. /// The verb is specified in the supplied file parameter in the call to IQueryAssociations::GetString.
  169. /// </summary>
  170. DropTarget = 17,
  171. /// <summary>
  172. /// For a verb invoked through COM and the IExecuteCommand interface, you can use this flag to retrieve the IExecuteCommand object's CLSID.
  173. /// This CLSID is registered in the verb's command subkey as the DelegateExecute entry.
  174. /// The verb is specified in the supplied file parameter in the call to IQueryAssociations::GetString.
  175. /// </summary>
  176. DelegateExecute = 18,
  177. /// <summary>(No description available on MSDN)</summary>
  178. /// <remarks>Introduced in Windows 8.</remarks>
  179. SupportedUriProtocols = 19,
  180. /// <summary>The maximum defined <see cref="AssociationString"/> value, used for validation purposes.</summary>
  181. Max = 20
  182. }
  183. /// <summary>Shell32 FileAttributes structure, used to retrieve the different types of a file system object.</summary>
  184. [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
  185. [SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue")]
  186. [Flags]
  187. public enum FileAttributes
  188. {
  189. /// <summary>0x000000000 - Get file system object large icon.</summary>
  190. /// <remarks>The <see cref="Icon"/> flag must also be set.</remarks>
  191. LargeIcon = 0,
  192. /// <summary>0x000000001 - Get file system object small icon.</summary>
  193. /// <remarks>The <see cref="Icon"/> flag must also be set.</remarks>
  194. SmallIcon = 1,
  195. /// <summary>0x000000002 - Get file system object open icon.</summary>
  196. /// <remarks>A container object displays an open icon to indicate that the container is open.</remarks>
  197. /// <remarks>The <see cref="Icon"/> and/or <see cref="SysIconIndex"/> flag must also be set.</remarks>
  198. OpenIcon = 2,
  199. /// <summary>0x000000004 - Get file system object Shell-sized icon.</summary>
  200. /// <remarks>If this attribute is not specified the function sizes the icon according to the system metric values.</remarks>
  201. ShellIconSize = 4,
  202. /// <summary>0x000000008 - Get file system object by its PIDL.</summary>
  203. /// <remarks>Indicate that the given file contains the address of an ITEMIDLIST structure rather than a path name.</remarks>
  204. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Pidl")]
  205. Pidl = 8,
  206. /// <summary>0x000000010 - Indicates that the given file should not be accessed. Rather, it should act as if the given file exists and use the supplied attributes.</summary>
  207. /// <remarks>This flag cannot be combined with the <see cref="Attributes"/>, <see cref="ExeType"/> or <see cref="Pidl"/> attributes.</remarks>
  208. UseFileAttributes = 16,
  209. /// <summary>0x000000020 - Apply the appropriate overlays to the file's icon.</summary>
  210. /// <remarks>The <see cref="Icon"/> flag must also be set.</remarks>
  211. AddOverlays = 32,
  212. /// <summary>0x000000040 - Returns the index of the overlay icon.</summary>
  213. /// <remarks>The value of the overlay index is returned in the upper eight bits of the iIcon member of the structure specified by psfi.</remarks>
  214. OverlayIndex = 64,
  215. /// <summary>0x000000100 - Retrieve the handle to the icon that represents the file and the index of the icon within the system image list. The handle is copied to the <see cref="FileInfo.IconHandle"/> member of the structure, and the index is copied to the <see cref="FileInfo.IconIndex"/> member.</summary>
  216. Icon = 256,
  217. /// <summary>0x000000200 - Retrieve the display name for the file. The name is copied to the <see cref="FileInfo.DisplayName"/> member of the structure.</summary>
  218. /// <remarks>The returned display name uses the long file name, if there is one, rather than the 8.3 form of the file name.</remarks>
  219. DisplayName = 512,
  220. /// <summary>0x000000400 - Retrieve the string that describes the file's type.</summary>
  221. TypeName = 1024,
  222. /// <summary>0x000000800 - Retrieve the item attributes. The attributes are copied to the <see cref="FileInfo.Attributes"/> member of the structure.</summary>
  223. /// <remarks>Will touch every file, degrading performance.</remarks>
  224. Attributes = 2048,
  225. /// <summary>0x000001000 - Retrieve the name of the file that contains the icon representing the file specified by pszPath. The name of the file containing the icon is copied to the <see cref="FileInfo.DisplayName"/> member of the structure. The icon's index is copied to that structure's <see cref="FileInfo.IconIndex"/> member.</summary>
  226. IconLocation = 4096,
  227. /// <summary>0x000002000 - Retrieve the type of the executable file if pszPath identifies an executable file.</summary>
  228. /// <remarks>This flag cannot be specified with any other attributes.</remarks>
  229. ExeType = 8192,
  230. /// <summary>0x000004000 - Retrieve the index of a system image list icon.</summary>
  231. SysIconIndex = 16384,
  232. /// <summary>0x000008000 - Add the link overlay to the file's icon.</summary>
  233. /// <remarks>The <see cref="Icon"/> flag must also be set.</remarks>
  234. LinkOverlay = 32768,
  235. /// <summary>0x000010000 - Blend the file's icon with the system highlight color.</summary>
  236. Selected = 65536,
  237. /// <summary>0x000020000 - Modify <see cref="Attributes"/> to indicate that <see cref="FileInfo.Attributes"/> contains specific attributes that are desired.</summary>
  238. /// <remarks>This flag cannot be specified with the <see cref="Icon"/> attribute. Will touch every file, degrading performance.</remarks>
  239. AttributesSpecified = 131072
  240. }
  241. /// <summary>SHFILEINFO structure, contains information about a file system object.</summary>
  242. [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Sh")]
  243. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Sh")]
  244. [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
  245. [SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")]
  246. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  247. public struct FileInfo
  248. {
  249. /// <summary>A handle to the icon that represents the file.</summary>
  250. /// <remarks>Caller is responsible for destroying this handle with DestroyIcon() when no longer needed.</remarks>
  251. [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
  252. public readonly IntPtr IconHandle;
  253. /// <summary>The index of the icon image within the system image list.</summary>
  254. [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
  255. public int IconIndex;
  256. /// <summary>An array of values that indicates the attributes of the file object.</summary>
  257. [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
  258. [MarshalAs(UnmanagedType.U4)]
  259. public readonly GetAttributesOf Attributes;
  260. /// <summary>The name of the file as it appears in the Windows Shell, or the path and file name of the file that contains the icon representing the file.</summary>
  261. [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
  262. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MaxPath)]
  263. public string DisplayName;
  264. /// <summary>The type of file.</summary>
  265. [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
  266. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
  267. public string TypeName;
  268. }
  269. /// <summary>SFGAO - Attributes that can be retrieved from a file system object.</summary>
  270. [SuppressMessage("Microsoft.Usage", "CA2217:DoNotMarkEnumsWithFlags"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
  271. [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Sh")]
  272. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Sh")]
  273. [SuppressMessage("Microsoft.Naming", "CA1714:FlagsEnumsShouldHavePluralNames")]
  274. [Flags]
  275. public enum GetAttributesOf
  276. {
  277. /// <summary>0x00000000 - None.</summary>
  278. None = 0,
  279. /// <summary>0x00000001 - The specified items can be copied.</summary>
  280. CanCopy = 1,
  281. /// <summary>0x00000002 - The specified items can be moved.</summary>
  282. CanMove = 2,
  283. /// <summary>0x00000004 - Shortcuts can be created for the specified items.</summary>
  284. CanLink = 4,
  285. /// <summary>0x00000008 - The specified items can be bound to an IStorage object through IShellFolder::BindToObject. For more information about namespace manipulation capabilities, see IStorage.</summary>
  286. Storage = 8,
  287. /// <summary>0x00000010 - The specified items can be renamed. Note that this value is essentially a suggestion; not all namespace clients allow items to be renamed. However, those that do must have this attribute set.</summary>
  288. CanRename = 16,
  289. /// <summary>0x00000020 - The specified items can be deleted.</summary>
  290. CanDelete = 32,
  291. /// <summary>0x00000040 - The specified items have property sheets.</summary>
  292. HasPropSheet = 64,
  293. /// <summary>0x00000100 - The specified items are drop targets.</summary>
  294. DropTarget = 256,
  295. /// <summary>0x00001000 - The specified items are system items.</summary>
  296. /// <remarks>Windows 7 and later.</remarks>
  297. System = 4096,
  298. /// <summary>0x00002000 - The specified items are encrypted and might require special presentation.</summary>
  299. Encrypted = 8192,
  300. /// <summary>0x00004000 - Accessing the item (through IStream or other storage interfaces) is expected to be a slow operation.</summary>
  301. IsSlow = 16384,
  302. /// <summary>0x00008000 - The specified items are shown as dimmed and unavailable to the user.</summary>
  303. Ghosted = 32768,
  304. /// <summary>0x00010000 - The specified items are shortcuts.</summary>
  305. Link = 65536,
  306. /// <summary>0x00020000 - The specified objects are shared.</summary>
  307. Share = 131072,
  308. /// <summary>0x00040000 - The specified items are read-only. In the case of folders, this means that new items cannot be created in those folders.</summary>
  309. ReadOnly = 262144,
  310. /// <summary>0x00080000 - The item is hidden and should not be displayed unless the Show hidden files and folders option is enabled in Folder Settings.</summary>
  311. Hidden = 524288,
  312. /// <summary>0x00100000 - The items are nonenumerated items and should be hidden. They are not returned through an enumerator such as that created by the IShellFolder::EnumObjects method.</summary>
  313. NonEnumerated = 1048576,
  314. /// <summary>0x00200000 - The items contain new content, as defined by the particular application.</summary>
  315. NewContent = 2097152,
  316. /// <summary>0x00400000 - Indicates that the item has a stream associated with it.</summary>
  317. Stream = 4194304,
  318. /// <summary>0x00800000 - Children of this item are accessible through IStream or IStorage.</summary>
  319. StorageAncestor = 8388608,
  320. /// <summary>0x01000000 - When specified as input, instructs the folder to validate that the items contained in a folder or Shell item array exist.</summary>
  321. Validate = 16777216,
  322. /// <summary>0x02000000 - The specified items are on removable media or are themselves removable devices.</summary>
  323. Removable = 33554432,
  324. /// <summary>0x04000000 - The specified items are compressed.</summary>
  325. Compressed = 67108864,
  326. /// <summary>0x08000000 - The specified items can be hosted inside a web browser or Windows Explorer frame.</summary>
  327. Browsable = 134217728,
  328. /// <summary>0x10000000 - The specified folders are either file system folders or contain at least one descendant (child, grandchild, or later) that is a file system folder.</summary>
  329. FileSysAncestor = 268435456,
  330. /// <summary>0x20000000 - The specified items are folders.</summary>
  331. Folder = 536870912,
  332. /// <summary>0x40000000 - The specified folders or files are part of the file system (that is, they are files, directories, or root directories).</summary>
  333. FileSystem = 1073741824,
  334. /// <summary>0x80000000 - The specified folders have subfolders.</summary>
  335. [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "SubFolder")]
  336. HasSubFolder = unchecked ((int)0x80000000)
  337. }
  338. /// <summary>Used by method UrlIs() to define a URL type.</summary>
  339. public enum UrlType
  340. {
  341. /// <summary>Is the URL valid?</summary>
  342. IsUrl = 0,
  343. /// <summary>Is the URL opaque?</summary>
  344. IsOpaque = 1,
  345. /// <summary>Is the URL a URL that is not typically tracked in navigation history?</summary>
  346. IsNoHistory = 2,
  347. /// <summary>Is the URL a file URL?</summary>
  348. IsFileUrl = 3,
  349. /// <summary>Attempt to determine a valid scheme for the URL.</summary>
  350. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Appliable")]
  351. IsAppliable = 4,
  352. /// <summary>Does the URL string end with a directory?</summary>
  353. IsDirectory = 5,
  354. /// <summary>Does the URL have an appended query string?</summary>
  355. IsHasQuery = 6
  356. }
  357. #region Methods
  358. /// <summary>Destroys an icon and frees any memory the icon occupied.</summary>
  359. /// <param name="iconHandle">An <see cref="IntPtr"/> handle to an icon.</param>
  360. public static void DestroyIcon(IntPtr iconHandle)
  361. {
  362. if (IntPtr.Zero != iconHandle)
  363. NativeMethods.DestroyIcon(iconHandle);
  364. }
  365. /// <summary>Gets the file or protocol that is associated with <paramref name="path"/> from the registry.</summary>
  366. /// <param name="path">A path to the file.</param>
  367. /// <returns>The associated file- or protocol-related string from the registry or <c>string.Empty</c> if no association can be found.</returns>
  368. [SecurityCritical]
  369. public static string GetFileAssociation(string path)
  370. {
  371. return GetFileAssociationCore(path, AssociationAttributes.Verify, AssociationString.Executable);
  372. }
  373. /// <summary>Gets the content-type that is associated with <paramref name="path"/> from the registry.</summary>
  374. /// <param name="path">A path to the file.</param>
  375. /// <returns>The associated file- or protocol-related content-type from the registry or <c>string.Empty</c> if no association can be found.</returns>
  376. [SecurityCritical]
  377. public static string GetFileContentType(string path)
  378. {
  379. return GetFileAssociationCore(path, AssociationAttributes.Verify, AssociationString.ContentType);
  380. }
  381. /// <summary>Gets the default icon that is associated with <paramref name="path"/> from the registry.</summary>
  382. /// <param name="path">A path to the file.</param>
  383. /// <returns>The associated file- or protocol-related default icon from the registry or <c>string.Empty</c> if no association can be found.</returns>
  384. [SecurityCritical]
  385. public static string GetFileDefaultIcon(string path)
  386. {
  387. return GetFileAssociationCore(path, AssociationAttributes.Verify, AssociationString.DefaultIcon);
  388. }
  389. /// <summary>Gets the friendly application name that is associated with <paramref name="path"/> from the registry.</summary>
  390. /// <param name="path">A path to the file.</param>
  391. /// <returns>The associated file- or protocol-related friendly application name from the registry or <c>string.Empty</c> if no association can be found.</returns>
  392. [SecurityCritical]
  393. public static string GetFileFriendlyAppName(string path)
  394. {
  395. return GetFileAssociationCore(path, AssociationAttributes.InitByExeName, AssociationString.FriendlyAppName);
  396. }
  397. /// <summary>Gets the friendly document name that is associated with <paramref name="path"/> from the registry.</summary>
  398. /// <param name="path">A path to the file.</param>
  399. /// <returns>The associated file- or protocol-related friendly document name from the registry or <c>string.Empty</c> if no association can be found.</returns>
  400. [SecurityCritical]
  401. public static string GetFileFriendlyDocName(string path)
  402. {
  403. return GetFileAssociationCore(path, AssociationAttributes.Verify, AssociationString.FriendlyDocName);
  404. }
  405. /// <summary>Gets an <see cref="IntPtr"/> handle to the Shell icon that represents the file.</summary>
  406. /// <remarks>Caller is responsible for destroying this handle with DestroyIcon() when no longer needed.</remarks>
  407. /// <param name="filePath">
  408. /// The path to the file system object which should not exceed maximum path length. Both absolute and
  409. /// relative paths are valid.
  410. /// </param>
  411. /// <param name="iconAttributes">
  412. /// Icon size <see cref="Shell32.FileAttributes.SmallIcon"/> or <see cref="Shell32.FileAttributes.LargeIcon"/>. Can also be combined
  413. /// with <see cref="Shell32.FileAttributes.AddOverlays"/> and others.
  414. /// </param>
  415. /// <returns>An <see cref="IntPtr"/> handle to the Shell icon that represents the file, or IntPtr.Zero on failure.</returns>
  416. [SecurityCritical]
  417. public static IntPtr GetFileIcon(string filePath, FileAttributes iconAttributes)
  418. {
  419. if (Utils.IsNullOrWhiteSpace(filePath))
  420. return IntPtr.Zero;
  421. var fileInfo = GetFileInfoCore(filePath, System.IO.FileAttributes.Normal, FileAttributes.Icon | iconAttributes, true, true);
  422. return fileInfo.IconHandle == IntPtr.Zero ? IntPtr.Zero : fileInfo.IconHandle;
  423. }
  424. /// <summary>Retrieves information about an object in the file system, such as a file, folder, directory, or drive root.</summary>
  425. /// <returns>A <see cref="Shell32.FileInfo"/> struct instance.</returns>
  426. /// <remarks>
  427. /// <para>You should call this function from a background thread.</para>
  428. /// <para>Failure to do so could cause the UI to stop responding.</para>
  429. /// <para>Unicode path are supported.</para>
  430. /// </remarks>
  431. /// <param name="filePath">The path to the file system object which should not exceed the maximum path length. Both absolute and relative paths are valid.</param>
  432. /// <param name="attributes">A <see cref="System.IO.FileAttributes"/> attribute.</param>
  433. /// <param name="fileAttributes">One ore more <see cref="FileAttributes"/> attributes.</param>
  434. /// <param name="continueOnException">
  435. /// <para><see langword="true"/> suppress any Exception that might be thrown as a result from a failure,</para>
  436. /// <para>such as ACLs protected directories or non-accessible reparse points.</para>
  437. /// </param>
  438. [SecurityCritical]
  439. public static FileInfo GetFileInfo(string filePath, System.IO.FileAttributes attributes, FileAttributes fileAttributes, bool continueOnException)
  440. {
  441. return GetFileInfoCore(filePath, attributes, fileAttributes, true, continueOnException);
  442. }
  443. /// <summary>Retrieves an instance of <see cref="Shell32Info"/> containing information about the specified file.</summary>
  444. /// <param name="path">A path to the file.</param>
  445. /// <returns>A <see cref="Shell32Info"/> class instance.</returns>
  446. [SecurityCritical]
  447. public static Shell32Info GetShell32Info(string path)
  448. {
  449. return new Shell32Info(path);
  450. }
  451. /// <summary>Retrieves an instance of <see cref="Shell32Info"/> containing information about the specified file.</summary>
  452. /// <param name="path">A path to the file.</param>
  453. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  454. /// <returns>A <see cref="Shell32Info"/> class instance.</returns>
  455. [SecurityCritical]
  456. public static Shell32Info GetShell32Info(string path, PathFormat pathFormat)
  457. {
  458. return new Shell32Info(path, pathFormat);
  459. }
  460. /// <summary>Gets the "Open With" command that is associated with <paramref name="path"/> from the registry.</summary>
  461. /// <param name="path">A path to the file.</param>
  462. /// <returns>The associated file- or protocol-related "Open With" command from the registry or <c>string.Empty</c> if no association can be found.</returns>
  463. [SecurityCritical]
  464. public static string GetFileOpenWithAppName(string path)
  465. {
  466. return GetFileAssociationCore(path, AssociationAttributes.Verify, AssociationString.FriendlyAppName);
  467. }
  468. /// <summary>Gets the Shell command that is associated with <paramref name="path"/> from the registry.</summary>
  469. /// <param name="path">A path to the file.</param>
  470. /// <returns>The associated file- or protocol-related Shell command from the registry or <c>string.Empty</c> if no association can be found.</returns>
  471. [SecurityCritical]
  472. public static string GetFileVerbCommand(string path)
  473. {
  474. return GetFileAssociationCore(path, AssociationAttributes.Verify, AssociationString.Command);
  475. }
  476. /// <summary>Converts a file URL to a Microsoft MS-DOS path.</summary>
  477. /// <param name="urlPath">The file URL.</param>
  478. /// <returns>
  479. /// <para>The Microsoft MS-DOS path. If no path can be created, <c>string.Empty</c> is returned.</para>
  480. /// <para>If <paramref name="urlPath"/> is <see langword="null"/>, <see langword="null"/> will also be returned.</para>
  481. /// </returns>
  482. [SecurityCritical]
  483. internal static string PathCreateFromUrl(string urlPath)
  484. {
  485. if (urlPath == null)
  486. return null;
  487. var buffer = new StringBuilder(NativeMethods.MaxPathUnicode);
  488. var bufferSize = (uint) buffer.Capacity;
  489. var lastError = NativeMethods.PathCreateFromUrl(urlPath, buffer, ref bufferSize, 0);
  490. // Don't throw exception, but return string.Empty;
  491. return lastError == Win32Errors.S_OK ? buffer.ToString() : string.Empty;
  492. }
  493. /// <summary>Creates a path from a file URL.</summary>
  494. /// <param name="urlPath">The URL.</param>
  495. /// <returns>
  496. /// <para>The file path. If no path can be created, <c>string.Empty</c> is returned.</para>
  497. /// <para>If <paramref name="urlPath"/> is <see langword="null"/>, <see langword="null"/> will also be returned.</para>
  498. /// </returns>
  499. [SecurityCritical]
  500. internal static string PathCreateFromUrlAlloc(string urlPath)
  501. {
  502. if (!NativeMethods.IsAtLeastWindowsVista)
  503. throw new PlatformNotSupportedException(Resources.Requires_Windows_Vista_Or_Higher);
  504. if (urlPath == null)
  505. return null;
  506. StringBuilder buffer;
  507. var lastError = NativeMethods.PathCreateFromUrlAlloc(urlPath, out buffer, 0);
  508. // Don't throw exception, but return string.Empty;
  509. return lastError == Win32Errors.S_OK ? buffer.ToString() : string.Empty;
  510. }
  511. /// <summary>Determines whether a path to a file system object such as a file or folder is valid.</summary>
  512. /// <param name="path">The full path of maximum length the maximum path length to the object to verify.</param>
  513. /// <returns><see langword="true"/> if the file exists; <see langword="false"/> otherwise</returns>
  514. [SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "lastError")]
  515. [SecurityCritical]
  516. public static bool PathFileExists(string path)
  517. {
  518. // PathFileExists()
  519. // In the ANSI version of this function, the name is limited to 248 characters.
  520. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  521. // 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
  522. return !Utils.IsNullOrWhiteSpace(path) && NativeMethods.PathFileExists(Path.GetFullPathCore(null, path, GetFullPathOptions.AsLongPath | GetFullPathOptions.FullCheck | GetFullPathOptions.ContinueOnNonExist));
  523. }
  524. /// <summary>Tests whether a URL is a specified type.</summary>
  525. /// <param name="url">The URL.</param>
  526. /// <param name="urlType"></param>
  527. /// <returns>
  528. /// For all but one of the URL types, UrlIs returns <see langword="true"/> if the URL is the specified type, or <see langword="false"/> otherwise.
  529. /// If UrlIs is set to <see cref="UrlType.IsAppliable"/>, UrlIs will attempt to determine the URL scheme.
  530. /// If the function is able to determine a scheme, it returns <see langword="true"/>, or <see langword="false"/> otherwise.
  531. /// </returns>
  532. [SecurityCritical]
  533. internal static bool UrlIs(string url, UrlType urlType)
  534. {
  535. return NativeMethods.UrlIs(url, urlType);
  536. }
  537. /// <summary>Converts a Microsoft MS-DOS path to a canonicalized URL.</summary>
  538. /// <param name="path">The full MS-DOS path of maximum length <see cref="NativeMethods.MaxPath"/>.</param>
  539. /// <returns>
  540. /// <para>The URL. If no URL can be created <c>string.Empty</c> is returned.</para>
  541. /// <para>If <paramref name="path"/> is <see langword="null"/>, <see langword="null"/> will also be returned.</para>
  542. /// </returns>
  543. [SecurityCritical]
  544. internal static string UrlCreateFromPath(string path)
  545. {
  546. if (path == null)
  547. return null;
  548. // UrlCreateFromPath does not support extended paths.
  549. var pathRp = Path.GetRegularPathCore(path, GetFullPathOptions.CheckInvalidPathChars, false);
  550. var buffer = new StringBuilder(NativeMethods.MaxPathUnicode);
  551. var bufferSize = (uint) buffer.Capacity;
  552. var lastError = NativeMethods.UrlCreateFromPath(pathRp, buffer, ref bufferSize, 0);
  553. // Don't throw exception, but return null;
  554. var url = buffer.ToString();
  555. if (Utils.IsNullOrWhiteSpace(url))
  556. url = string.Empty;
  557. return lastError == Win32Errors.S_OK ? url : string.Empty;
  558. }
  559. /// <summary>Tests a URL to determine if it is a file URL.</summary>
  560. /// <param name="url">The URL.</param>
  561. /// <returns><see langword="true"/> if the URL is a file URL, or <see langword="false"/> otherwise.</returns>
  562. [SecurityCritical]
  563. internal static bool UrlIsFileUrl(string url)
  564. {
  565. return NativeMethods.UrlIs(url, UrlType.IsFileUrl);
  566. }
  567. /// <summary>Returns whether a URL is a URL that browsers typically do not include in navigation history.</summary>
  568. /// <param name="url">The URL.</param>
  569. /// <returns><see langword="true"/> if the URL is a URL that is not included in navigation history, or <see langword="false"/> otherwise.</returns>
  570. [SecurityCritical]
  571. internal static bool UrlIsNoHistory(string url)
  572. {
  573. return NativeMethods.UrlIs(url, UrlType.IsNoHistory);
  574. }
  575. /// <summary>Returns whether a URL is opaque.</summary>
  576. /// <param name="url">The URL.</param>
  577. /// <returns><see langword="true"/> if the URL is opaque, or <see langword="false"/> otherwise.</returns>
  578. [SecurityCritical]
  579. internal static bool UrlIsOpaque(string url)
  580. {
  581. return NativeMethods.UrlIs(url, UrlType.IsOpaque);
  582. }
  583. #region Internal Methods
  584. /// <summary>Searches for and retrieves a file or protocol association-related string from the registry.</summary>
  585. /// <param name="path">A path to a file.</param>
  586. /// <param name="attributes">One or more <see cref="AssociationAttributes"/> attributes. Only one "InitXXX" attribute can be used.</param>
  587. /// <param name="associationType">A <see cref="AssociationString"/> attribute.</param>
  588. /// <returns>The associated file- or protocol-related string from the registry or <c>string.Empty</c> if no association can be found.</returns>
  589. /// <exception cref="ArgumentNullException"/>
  590. [SecurityCritical]
  591. private static string GetFileAssociationCore(string path, AssociationAttributes attributes, AssociationString associationType)
  592. {
  593. if (Utils.IsNullOrWhiteSpace(path))
  594. throw new ArgumentNullException("path");
  595. attributes = attributes | AssociationAttributes.NoTruncate | AssociationAttributes.RemapRunDll;
  596. uint bufferSize = NativeMethods.MaxPath;
  597. StringBuilder buffer;
  598. uint retVal;
  599. do
  600. {
  601. buffer = new StringBuilder((int)bufferSize);
  602. // AssocQueryString()
  603. // In the ANSI version of this function, the name is limited to 248 characters.
  604. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  605. // 2014-02-05: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
  606. // However, the function fails when using Unicode format.
  607. retVal = NativeMethods.AssocQueryString(attributes, associationType, path, null, buffer, out bufferSize);
  608. // No Exception is thrown, just return empty string on error.
  609. //switch (retVal)
  610. //{
  611. // // 0x80070483: No application is associated with the specified file for this operation.
  612. // case 2147943555:
  613. // case Win32Errors.E_POINTER:
  614. // case Win32Errors.S_OK:
  615. // break;
  616. // default:
  617. // NativeError.ThrowException(retVal);
  618. // break;
  619. //}
  620. } while (retVal == Win32Errors.E_POINTER);
  621. return buffer.ToString();
  622. }
  623. /// <summary>Retrieve information about an object in the file system, such as a file, folder, directory, or drive root.</summary>
  624. /// <returns>A <see cref="Shell32.FileInfo"/> struct instance.</returns>
  625. /// <remarks>
  626. /// <para>You should call this function from a background thread.</para>
  627. /// <para>Failure to do so could cause the UI to stop responding.</para>
  628. /// <para>Unicode path are not supported.</para>
  629. /// </remarks>
  630. /// <param name="path">The path to the file system object which should not exceed the maximum path length in length. Both absolute and relative paths are valid.</param>
  631. /// <param name="attributes">A <see cref="System.IO.FileAttributes"/> attribute.</param>
  632. /// <param name="fileAttributes">A <see cref="FileAttributes"/> attribute.</param>
  633. /// <param name="checkInvalidPathChars">Checks that the path contains only valid path-characters.</param>
  634. /// <param name="continueOnException">
  635. /// <para><see langword="true"/> suppress any Exception that might be thrown as a result from a failure,</para>
  636. /// <para>such as ACLs protected directories or non-accessible reparse points.</para>
  637. /// </param>
  638. [SecurityCritical]
  639. internal static FileInfo GetFileInfoCore(string path, System.IO.FileAttributes attributes, FileAttributes fileAttributes, bool checkInvalidPathChars, bool continueOnException)
  640. {
  641. // Prevent possible crash.
  642. var fileInfo = new FileInfo
  643. {
  644. DisplayName = string.Empty,
  645. TypeName = string.Empty,
  646. IconIndex = 0
  647. };
  648. if (!Utils.IsNullOrWhiteSpace(path))
  649. {
  650. // ShGetFileInfo()
  651. // In the ANSI version of this function, the name is limited to 248 characters.
  652. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  653. // 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
  654. // However, the function fails when using Unicode format.
  655. var shGetFileInfo = NativeMethods.ShGetFileInfo(Path.GetRegularPathCore(path, checkInvalidPathChars ? GetFullPathOptions.CheckInvalidPathChars : 0, false), attributes, out fileInfo, (uint) Marshal.SizeOf(fileInfo), fileAttributes);
  656. if (shGetFileInfo == IntPtr.Zero && !continueOnException)
  657. NativeError.ThrowException(Marshal.GetLastWin32Error(), path);
  658. }
  659. return fileInfo;
  660. }
  661. #endregion // Internal Methods
  662. #endregion // Methods
  663. }
  664. }