Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

846 rader
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. if (Utils.IsNullOrWhiteSpace(path))
  519. return false;
  520. // PathFileExists()
  521. // In the ANSI version of this function, the name is limited to 248 characters.
  522. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  523. // 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
  524. return NativeMethods.PathFileExists(Path.GetFullPathCore(null, path, GetFullPathOptions.AsLongPath | GetFullPathOptions.FullCheck | GetFullPathOptions.ContinueOnNonExist));
  525. }
  526. /// <summary>Tests whether a URL is a specified type.</summary>
  527. /// <param name="url">The URL.</param>
  528. /// <param name="urlType"></param>
  529. /// <returns>
  530. /// 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.
  531. /// If UrlIs is set to <see cref="UrlType.IsAppliable"/>, UrlIs will attempt to determine the URL scheme.
  532. /// If the function is able to determine a scheme, it returns <see langword="true"/>, or <see langword="false"/> otherwise.
  533. /// </returns>
  534. [SecurityCritical]
  535. internal static bool UrlIs(string url, UrlType urlType)
  536. {
  537. return NativeMethods.UrlIs(url, urlType);
  538. }
  539. /// <summary>Converts a Microsoft MS-DOS path to a canonicalized URL.</summary>
  540. /// <param name="path">The full MS-DOS path of maximum length <see cref="NativeMethods.MaxPath"/>.</param>
  541. /// <returns>
  542. /// <para>The URL. If no URL can be created <c>string.Empty</c> is returned.</para>
  543. /// <para>If <paramref name="path"/> is <see langword="null"/>, <see langword="null"/> will also be returned.</para>
  544. /// </returns>
  545. [SecurityCritical]
  546. internal static string UrlCreateFromPath(string path)
  547. {
  548. if (path == null)
  549. return null;
  550. // UrlCreateFromPath does not support extended paths.
  551. var pathRp = Path.GetRegularPathCore(path, GetFullPathOptions.CheckInvalidPathChars, false);
  552. var buffer = new StringBuilder(NativeMethods.MaxPathUnicode);
  553. var bufferSize = (uint) buffer.Capacity;
  554. var lastError = NativeMethods.UrlCreateFromPath(pathRp, buffer, ref bufferSize, 0);
  555. // Don't throw exception, but return null;
  556. var url = buffer.ToString();
  557. if (Utils.IsNullOrWhiteSpace(url))
  558. url = string.Empty;
  559. return lastError == Win32Errors.S_OK ? url : string.Empty;
  560. }
  561. /// <summary>Tests a URL to determine if it is a file URL.</summary>
  562. /// <param name="url">The URL.</param>
  563. /// <returns><see langword="true"/> if the URL is a file URL, or <see langword="false"/> otherwise.</returns>
  564. [SecurityCritical]
  565. internal static bool UrlIsFileUrl(string url)
  566. {
  567. return NativeMethods.UrlIs(url, UrlType.IsFileUrl);
  568. }
  569. /// <summary>Returns whether a URL is a URL that browsers typically do not include in navigation history.</summary>
  570. /// <param name="url">The URL.</param>
  571. /// <returns><see langword="true"/> if the URL is a URL that is not included in navigation history, or <see langword="false"/> otherwise.</returns>
  572. [SecurityCritical]
  573. internal static bool UrlIsNoHistory(string url)
  574. {
  575. return NativeMethods.UrlIs(url, UrlType.IsNoHistory);
  576. }
  577. /// <summary>Returns whether a URL is opaque.</summary>
  578. /// <param name="url">The URL.</param>
  579. /// <returns><see langword="true"/> if the URL is opaque, or <see langword="false"/> otherwise.</returns>
  580. [SecurityCritical]
  581. internal static bool UrlIsOpaque(string url)
  582. {
  583. return NativeMethods.UrlIs(url, UrlType.IsOpaque);
  584. }
  585. #region Internal Methods
  586. /// <summary>Searches for and retrieves a file or protocol association-related string from the registry.</summary>
  587. /// <param name="path">A path to a file.</param>
  588. /// <param name="attributes">One or more <see cref="AssociationAttributes"/> attributes. Only one "InitXXX" attribute can be used.</param>
  589. /// <param name="associationType">A <see cref="AssociationString"/> attribute.</param>
  590. /// <returns>The associated file- or protocol-related string from the registry or <c>string.Empty</c> if no association can be found.</returns>
  591. /// <exception cref="ArgumentNullException"/>
  592. [SecurityCritical]
  593. private static string GetFileAssociationCore(string path, AssociationAttributes attributes, AssociationString associationType)
  594. {
  595. if (Utils.IsNullOrWhiteSpace(path))
  596. throw new ArgumentNullException("path");
  597. attributes = attributes | AssociationAttributes.NoTruncate | AssociationAttributes.RemapRunDll;
  598. uint bufferSize = NativeMethods.MaxPath;
  599. StringBuilder buffer;
  600. uint retVal;
  601. do
  602. {
  603. buffer = new StringBuilder((int)bufferSize);
  604. // AssocQueryString()
  605. // In the ANSI version of this function, the name is limited to 248 characters.
  606. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  607. // 2014-02-05: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
  608. // However, the function fails when using Unicode format.
  609. retVal = NativeMethods.AssocQueryString(attributes, associationType, path, null, buffer, out bufferSize);
  610. // No Exception is thrown, just return empty string on error.
  611. //switch (retVal)
  612. //{
  613. // // 0x80070483: No application is associated with the specified file for this operation.
  614. // case 2147943555:
  615. // case Win32Errors.E_POINTER:
  616. // case Win32Errors.S_OK:
  617. // break;
  618. // default:
  619. // NativeError.ThrowException(retVal);
  620. // break;
  621. //}
  622. } while (retVal == Win32Errors.E_POINTER);
  623. return buffer.ToString();
  624. }
  625. /// <summary>Retrieve information about an object in the file system, such as a file, folder, directory, or drive root.</summary>
  626. /// <returns>A <see cref="Shell32.FileInfo"/> struct instance.</returns>
  627. /// <remarks>
  628. /// <para>You should call this function from a background thread.</para>
  629. /// <para>Failure to do so could cause the UI to stop responding.</para>
  630. /// <para>Unicode path are not supported.</para>
  631. /// </remarks>
  632. /// <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>
  633. /// <param name="attributes">A <see cref="System.IO.FileAttributes"/> attribute.</param>
  634. /// <param name="fileAttributes">A <see cref="FileAttributes"/> attribute.</param>
  635. /// <param name="checkInvalidPathChars">Checks that the path contains only valid path-characters.</param>
  636. /// <param name="continueOnException">
  637. /// <para><see langword="true"/> suppress any Exception that might be thrown as a result from a failure,</para>
  638. /// <para>such as ACLs protected directories or non-accessible reparse points.</para>
  639. /// </param>
  640. [SecurityCritical]
  641. internal static FileInfo GetFileInfoCore(string path, System.IO.FileAttributes attributes, FileAttributes fileAttributes, bool checkInvalidPathChars, bool continueOnException)
  642. {
  643. // Prevent possible crash.
  644. var fileInfo = new FileInfo
  645. {
  646. DisplayName = string.Empty,
  647. TypeName = string.Empty,
  648. IconIndex = 0
  649. };
  650. if (!Utils.IsNullOrWhiteSpace(path))
  651. {
  652. // ShGetFileInfo()
  653. // In the ANSI version of this function, the name is limited to 248 characters.
  654. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  655. // 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
  656. // However, the function fails when using Unicode format.
  657. var shGetFileInfo = NativeMethods.ShGetFileInfo(Path.GetRegularPathCore(path, checkInvalidPathChars ? GetFullPathOptions.CheckInvalidPathChars : 0, false), attributes, out fileInfo, (uint) Marshal.SizeOf(fileInfo), fileAttributes);
  658. if (shGetFileInfo == IntPtr.Zero && !continueOnException)
  659. NativeError.ThrowException(Marshal.GetLastWin32Error(), path);
  660. }
  661. return fileInfo;
  662. }
  663. #endregion // Internal Methods
  664. #endregion // Methods
  665. }
  666. }