| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 5 | * Modified By: Garrett Trant <gtrant@users.sourceforge.net> |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.Text; |
|---|
| 25 | using System.Runtime.InteropServices; |
|---|
| 26 | using System.Windows.Forms; |
|---|
| 27 | |
|---|
| 28 | namespace Eraser.Util |
|---|
| 29 | { |
|---|
| 30 | public static class ShellApi |
|---|
| 31 | { |
|---|
| 32 | /// <summary> |
|---|
| 33 | /// Empties the recycle bin for the current user. |
|---|
| 34 | /// </summary> |
|---|
| 35 | /// <param name="options">The list of flags to pass to the shell regarding |
|---|
| 36 | /// the user feedback, etc.</param> |
|---|
| 37 | public static void EmptyRecycleBin(EmptyRecycleBinOptions options) |
|---|
| 38 | { |
|---|
| 39 | NativeMethods.SHEmptyRecycleBin(IntPtr.Zero, null, |
|---|
| 40 | (NativeMethods.SHEmptyRecycleBinFlags)options); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /// <summary> |
|---|
| 44 | /// Encapsulates all functions, structs and constants from Shell32.dll |
|---|
| 45 | /// </summary> |
|---|
| 46 | internal static class NativeMethods |
|---|
| 47 | { |
|---|
| 48 | /// <summary> |
|---|
| 49 | /// Truncates a path to fit within a certain number of characters by |
|---|
| 50 | /// replacing path components with ellipses. |
|---|
| 51 | /// </summary> |
|---|
| 52 | /// <param name="pszOut">[out] The address of the string that has been altered.</param> |
|---|
| 53 | /// <param name="pszSrc">[in] A pointer to a null-terminated string of maximum |
|---|
| 54 | /// length MAX_PATH that contains the path to be altered.</param> |
|---|
| 55 | /// <param name="cchMax">[in] The maximum number of characters to be |
|---|
| 56 | /// contained in the new string, including the terminating NULL character. |
|---|
| 57 | /// For example, if cchMax = 8, the resulting string can contain a maximum |
|---|
| 58 | /// of 7 characters plus the terminating NULL character.</param> |
|---|
| 59 | /// <param name="dwFlags">Reserved.</param> |
|---|
| 60 | /// <returns>Returns TRUE if successful, or FALSE otherwise.</returns> |
|---|
| 61 | [DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)] |
|---|
| 62 | [return: MarshalAs(UnmanagedType.Bool)] |
|---|
| 63 | public static extern bool PathCompactPathEx(StringBuilder pszOut, |
|---|
| 64 | string pszSrc, uint cchMax, uint dwFlags); |
|---|
| 65 | |
|---|
| 66 | /// <summary> |
|---|
| 67 | /// Empties the Recycle Bin on the specified drive. |
|---|
| 68 | /// </summary> |
|---|
| 69 | /// <param name="hwnd">A handle to the parent window of any dialog boxes |
|---|
| 70 | /// that might be displayed during the operation. This parameter can be |
|---|
| 71 | /// NULL.</param> |
|---|
| 72 | /// <param name="pszRootPath">The address of a null-terminated string of |
|---|
| 73 | /// maximum length MAX_PATH that contains the path of the root drive on |
|---|
| 74 | /// which the Recycle Bin is located. This parameter can contain the address |
|---|
| 75 | /// of a string formatted with the drive, folder, and subfolder names, for |
|---|
| 76 | /// example c:\windows\system\. It can also contain an empty string or NULL. |
|---|
| 77 | /// If this value is an empty string or NULL, all Recycle Bins on all drives |
|---|
| 78 | /// will be emptied.</param> |
|---|
| 79 | /// <param name="dwFlags">One or more of the SHEmptyRecycleBinFlags</param> |
|---|
| 80 | /// <returns>Returns S_OK if successful, or a COM-defined error value |
|---|
| 81 | /// otherwise.</returns> |
|---|
| 82 | [DllImport("Shell32.dll", CharSet = CharSet.Unicode)] |
|---|
| 83 | public static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, |
|---|
| 84 | SHEmptyRecycleBinFlags dwFlags); |
|---|
| 85 | |
|---|
| 86 | public enum SHEmptyRecycleBinFlags : uint |
|---|
| 87 | { |
|---|
| 88 | SHERB_NOCONFIRMATION = 0x00000001, |
|---|
| 89 | SHERB_NOPROGRESSUI = 0x00000002, |
|---|
| 90 | SHERB_NOSOUND = 0x00000004 |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /// <summary> |
|---|
| 94 | /// Retrieves information about an object in the file system, such as a |
|---|
| 95 | /// file, folder, directory, or drive root. |
|---|
| 96 | /// </summary> |
|---|
| 97 | /// <param name="path">[in] A pointer to a null-terminated string of maximum |
|---|
| 98 | /// length MAX_PATH that contains the path and file name. Both absolute |
|---|
| 99 | /// and relative paths are valid. |
|---|
| 100 | /// |
|---|
| 101 | /// If the uFlags parameter includes the SHGFI_PIDL flag, this parameter |
|---|
| 102 | /// must be the address of an ITEMIDLIST (PIDL) structure that contains |
|---|
| 103 | /// the list of item identifiers that uniquely identifies the file within |
|---|
| 104 | /// the Shell's namespace. The pointer to an item identifier list (PIDL) |
|---|
| 105 | /// must be a fully qualified PIDL. Relative PIDLs are not allowed. |
|---|
| 106 | /// |
|---|
| 107 | /// If the uFlags parameter includes the SHGFI_USEFILEATTRIBUTES flag, |
|---|
| 108 | /// this parameter does not have to be a valid file name. The function |
|---|
| 109 | /// will proceed as if the file exists with the specified name and with |
|---|
| 110 | /// the file attributes passed in the dwFileAttributes parameter. This |
|---|
| 111 | /// allows you to obtain information about a file type by passing just |
|---|
| 112 | /// the extension for pszPath and passing FILE_ATTRIBUTE_NORMAL in |
|---|
| 113 | /// dwFileAttributes. |
|---|
| 114 | /// |
|---|
| 115 | /// This string can use either short (the 8.3 form) or long file names.</param> |
|---|
| 116 | /// <param name="fileAttributes">[in] A combination of one or more file |
|---|
| 117 | /// attribute flags (FILE_ATTRIBUTE_ values as defined in Winnt.h). If |
|---|
| 118 | /// uFlags does not include the SHGFI_USEFILEATTRIBUTES flag, this |
|---|
| 119 | /// parameter is ignored.</param> |
|---|
| 120 | /// <param name="psfi">[out] The address of a SHFILEINFO structure to |
|---|
| 121 | /// receive the file information.</param> |
|---|
| 122 | /// <param name="cbFileInfo">[in] The size, in bytes, of the SHFILEINFO |
|---|
| 123 | /// structure pointed to by the psfi parameter.</param> |
|---|
| 124 | /// <param name="uFlags">[in] The flags that specify the file information |
|---|
| 125 | /// to retrieve. |
|---|
| 126 | /// This parameter can be a combination of the values in SHGetFileInfoFlags</param> |
|---|
| 127 | /// <returns>Returns a value whose meaning depends on the uFlags parameter. |
|---|
| 128 | /// |
|---|
| 129 | /// If uFlags does not contain SHGFI_EXETYPE or SHGFI_SYSICONINDEX, the return |
|---|
| 130 | /// value is nonzero if successful, or zero otherwise. |
|---|
| 131 | /// |
|---|
| 132 | /// If uFlags contains the SHGFI_EXETYPE flag, the return value specifies |
|---|
| 133 | /// the type of the executable file. It will be one of the following values. |
|---|
| 134 | /// 0 Nonexecutable file or an error condition. |
|---|
| 135 | /// LOWORD = NE or PE and HIWORD = Windows version Microsoft Windows application. |
|---|
| 136 | /// LOWORD = MZ and HIWORD = 0 Windows 95, Windows 98: Microsoft MS-DOS .exe, .com, or .bat file |
|---|
| 137 | /// Microsoft Windows NT, Windows 2000, Windows XP: MS-DOS .exe or .com file |
|---|
| 138 | /// LOWORD = PE and HIWORD = 0 Windows 95, Windows 98: Microsoft Win32 console application |
|---|
| 139 | /// Windows NT, Windows 2000, Windows XP: Win32 console application or .bat file |
|---|
| 140 | /// </returns> |
|---|
| 141 | [DllImport("Shell32.dll", CharSet = CharSet.Unicode)] |
|---|
| 142 | public static extern IntPtr SHGetFileInfo(string path, uint fileAttributes, |
|---|
| 143 | ref SHFILEINFO psfi, int cbFileInfo, SHGetFileInfoFlags uFlags); |
|---|
| 144 | |
|---|
| 145 | public enum SHGetFileInfoFlags |
|---|
| 146 | { |
|---|
| 147 | /// <summary> |
|---|
| 148 | /// Retrieve the handle to the icon that represents the file and the |
|---|
| 149 | /// index of the icon within the system image list. The handle is |
|---|
| 150 | /// copied to the hIcon member of the structure specified by psfi, |
|---|
| 151 | /// and the index is copied to the iIcon member. |
|---|
| 152 | /// </summary> |
|---|
| 153 | SHGFI_ICON = 0x000000100, |
|---|
| 154 | |
|---|
| 155 | /// <summary> |
|---|
| 156 | /// Retrieve the display name for the file. The name is copied to the |
|---|
| 157 | /// szDisplayName member of the structure specified in psfi. The returned |
|---|
| 158 | /// display name uses the long file name, if there is one, rather than |
|---|
| 159 | /// the 8.3 form of the file name. |
|---|
| 160 | /// </summary> |
|---|
| 161 | SHGFI_DISPLAYNAME = 0x000000200, |
|---|
| 162 | |
|---|
| 163 | /// <summary> |
|---|
| 164 | /// Retrieve the string that describes the file's type. The string |
|---|
| 165 | /// is copied to the szTypeName member of the structure specified in |
|---|
| 166 | /// psfi. |
|---|
| 167 | /// </summary> |
|---|
| 168 | SHGFI_TYPENAME = 0x000000400, |
|---|
| 169 | |
|---|
| 170 | /// <summary> |
|---|
| 171 | /// Retrieve the item attributes. The attributes are copied to the |
|---|
| 172 | /// dwAttributes member of the structure specified in the psfi parameter. |
|---|
| 173 | /// These are the same attributes that are obtained from |
|---|
| 174 | /// IShellFolder::GetAttributesOf. |
|---|
| 175 | /// </summary> |
|---|
| 176 | SHGFI_ATTRIBUTES = 0x000000800, |
|---|
| 177 | |
|---|
| 178 | /// <summary> |
|---|
| 179 | /// Retrieve the name of the file that contains the icon representing |
|---|
| 180 | /// the file specified by pszPath, as returned by the |
|---|
| 181 | /// IExtractIcon::GetIconLocation method of the file's icon handler. |
|---|
| 182 | /// Also retrieve the icon index within that file. The name of the |
|---|
| 183 | /// file containing the icon is copied to the szDisplayName member |
|---|
| 184 | /// of the structure specified by psfi. The icon's index is copied to |
|---|
| 185 | /// that structure's iIcon member. |
|---|
| 186 | /// </summary> |
|---|
| 187 | SHGFI_ICONLOCATION = 0x000001000, |
|---|
| 188 | |
|---|
| 189 | /// <summary> |
|---|
| 190 | /// Retrieve the type of the executable file if pszPath identifies an |
|---|
| 191 | /// executable file. The information is packed into the return value. |
|---|
| 192 | /// This flag cannot be specified with any other flags. |
|---|
| 193 | /// </summary> |
|---|
| 194 | SHGFI_EXETYPE = 0x000002000, |
|---|
| 195 | |
|---|
| 196 | /// <summary> |
|---|
| 197 | /// Retrieve the index of a system image list icon. If successful, |
|---|
| 198 | /// the index is copied to the iIcon member of psfi. The return value |
|---|
| 199 | /// is a handle to the system image list. Only those images whose |
|---|
| 200 | /// indices are successfully copied to iIcon are valid. Attempting |
|---|
| 201 | /// to access other images in the system image list will result in |
|---|
| 202 | /// undefined behavior. |
|---|
| 203 | /// </summary> |
|---|
| 204 | SHGFI_SYSICONINDEX = 0x000004000, |
|---|
| 205 | |
|---|
| 206 | /// <summary> |
|---|
| 207 | /// Modify SHGFI_ICON, causing the function to add the link overlay |
|---|
| 208 | /// to the file's icon. The SHGFI_ICON flag must also be set. |
|---|
| 209 | /// </summary> |
|---|
| 210 | SHGFI_LINKOVERLAY = 0x000008000, |
|---|
| 211 | |
|---|
| 212 | /// <summary> |
|---|
| 213 | /// Modify SHGFI_ICON, causing the function to blend the file's icon |
|---|
| 214 | /// with the system highlight color. The SHGFI_ICON flag must also |
|---|
| 215 | /// be set. |
|---|
| 216 | /// </summary> |
|---|
| 217 | SHGFI_SELECTED = 0x000010000, |
|---|
| 218 | |
|---|
| 219 | /// <summary> |
|---|
| 220 | /// Modify SHGFI_ATTRIBUTES to indicate that the dwAttributes member |
|---|
| 221 | /// of the SHFILEINFO structure at psfi contains the specific attributes |
|---|
| 222 | /// that are desired. These attributes are passed to IShellFolder::GetAttributesOf. |
|---|
| 223 | /// If this flag is not specified, 0xFFFFFFFF is passed to |
|---|
| 224 | /// IShellFolder::GetAttributesOf, requesting all attributes. This flag |
|---|
| 225 | /// cannot be specified with the SHGFI_ICON flag. |
|---|
| 226 | /// </summary> |
|---|
| 227 | SHGFI_ATTR_SPECIFIED = 0x000020000, |
|---|
| 228 | |
|---|
| 229 | /// <summary> |
|---|
| 230 | /// Modify SHGFI_ICON, causing the function to retrieve the file's |
|---|
| 231 | /// large icon. The SHGFI_ICON flag must also be set. |
|---|
| 232 | /// </summary> |
|---|
| 233 | SHGFI_LARGEICON = 0x000000000, |
|---|
| 234 | |
|---|
| 235 | /// <summary> |
|---|
| 236 | /// Modify SHGFI_ICON, causing the function to retrieve the file's |
|---|
| 237 | /// small icon. Also used to modify SHGFI_SYSICONINDEX, causing the |
|---|
| 238 | /// function to return the handle to the system image list that |
|---|
| 239 | /// contains small icon images. The SHGFI_ICON and/or |
|---|
| 240 | /// SHGFI_SYSICONINDEX flag must also be set. |
|---|
| 241 | /// </summary> |
|---|
| 242 | SHGFI_SMALLICON = 0x000000001, |
|---|
| 243 | |
|---|
| 244 | /// <summary> |
|---|
| 245 | /// Modify SHGFI_ICON, causing the function to retrieve the file's |
|---|
| 246 | /// open icon. Also used to modify SHGFI_SYSICONINDEX, causing the |
|---|
| 247 | /// function to return the handle to the system image list that |
|---|
| 248 | /// contains the file's small open icon. A container object displays |
|---|
| 249 | /// an open icon to indicate that the container is open. The SHGFI_ICON |
|---|
| 250 | /// and/or SHGFI_SYSICONINDEX flag must also be set. |
|---|
| 251 | /// </summary> |
|---|
| 252 | SHGFI_OPENICON = 0x000000002, |
|---|
| 253 | |
|---|
| 254 | /// <summary> |
|---|
| 255 | /// Modify SHGFI_ICON, causing the function to retrieve a Shell-sized |
|---|
| 256 | /// icon. If this flag is not specified the function sizes the icon |
|---|
| 257 | /// according to the system metric values. The SHGFI_ICON flag must |
|---|
| 258 | /// also be set. |
|---|
| 259 | /// </summary> |
|---|
| 260 | SHGFI_SHELLICONSIZE = 0x000000004, |
|---|
| 261 | |
|---|
| 262 | /// <summary> |
|---|
| 263 | /// Indicate that pszPath is the address of an ITEMIDLIST structure |
|---|
| 264 | /// rather than a path name. |
|---|
| 265 | /// </summary> |
|---|
| 266 | SHGFI_PIDL = 0x000000008, |
|---|
| 267 | |
|---|
| 268 | /// <summary> |
|---|
| 269 | /// Indicates that the function should not attempt to access the file |
|---|
| 270 | /// specified by pszPath. Rather, it should act as if the file specified |
|---|
| 271 | /// by pszPath exists with the file attributes passed in dwFileAttributes. |
|---|
| 272 | /// This flag cannot be combined with the SHGFI_ATTRIBUTES, SHGFI_EXETYPE, |
|---|
| 273 | /// or SHGFI_PIDL flags. |
|---|
| 274 | /// </summary> |
|---|
| 275 | SHGFI_USEFILEATTRIBUTES = 0x000000010, |
|---|
| 276 | |
|---|
| 277 | /// <summary> |
|---|
| 278 | /// Version 5.0. Apply the appropriate overlays to the file's icon. |
|---|
| 279 | /// The SHGFI_ICON flag must also be set. |
|---|
| 280 | /// </summary> |
|---|
| 281 | SHGFI_ADDOVERLAYS = 0x000000020, |
|---|
| 282 | |
|---|
| 283 | /// <summary> |
|---|
| 284 | /// Version 5.0. Return the index of the overlay icon. The value of |
|---|
| 285 | /// the overlay index is returned in the upper eight bits of the iIcon |
|---|
| 286 | /// member of the structure specified by psfi. This flag requires that |
|---|
| 287 | /// the SHGFI_ICON be set as well. |
|---|
| 288 | /// </summary> |
|---|
| 289 | SHGFI_OVERLAYINDEX = 0x000000040 |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] |
|---|
| 293 | public struct SHFILEINFO |
|---|
| 294 | { |
|---|
| 295 | /// <summary> |
|---|
| 296 | /// A handle to the icon that represents the file. You are responsible |
|---|
| 297 | /// for destroying this handle with DestroyIcon when you no longer need it. |
|---|
| 298 | /// </summary> |
|---|
| 299 | public IntPtr hIcon; |
|---|
| 300 | |
|---|
| 301 | /// <summary> |
|---|
| 302 | /// The index of the icon image within the system image list. |
|---|
| 303 | /// </summary> |
|---|
| 304 | public int iIcon; |
|---|
| 305 | |
|---|
| 306 | /// <summary> |
|---|
| 307 | /// An array of values that indicates the attributes of the file object. |
|---|
| 308 | /// For information about these values, see the IShellFolder::GetAttributesOf |
|---|
| 309 | /// method. |
|---|
| 310 | /// </summary> |
|---|
| 311 | public uint dwAttributes; |
|---|
| 312 | |
|---|
| 313 | /// <summary> |
|---|
| 314 | /// A string that contains the name of the file as it appears in the |
|---|
| 315 | /// Microsoft Windows Shell, or the path and file name of the file |
|---|
| 316 | /// that contains the icon representing the file. |
|---|
| 317 | /// </summary> |
|---|
| 318 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] |
|---|
| 319 | public string szDisplayName; |
|---|
| 320 | |
|---|
| 321 | /// <summary> |
|---|
| 322 | /// A string that describes the type of file. |
|---|
| 323 | /// </summary> |
|---|
| 324 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] |
|---|
| 325 | public string szTypeName; |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | [Flags] |
|---|
| 331 | public enum EmptyRecycleBinOptions |
|---|
| 332 | { |
|---|
| 333 | /// <summary> |
|---|
| 334 | /// No flags specified. |
|---|
| 335 | /// </summary> |
|---|
| 336 | None = 0, |
|---|
| 337 | |
|---|
| 338 | /// <summary> |
|---|
| 339 | /// No dialog box confirming the deletion of the objects will be displayed. |
|---|
| 340 | /// </summary> |
|---|
| 341 | NoConfirmation = (int)ShellApi.NativeMethods.SHEmptyRecycleBinFlags.SHERB_NOCONFIRMATION, |
|---|
| 342 | |
|---|
| 343 | /// <summary> |
|---|
| 344 | /// No dialog box indicating the progress will be displayed. |
|---|
| 345 | /// </summary> |
|---|
| 346 | NoProgressUI = (int)ShellApi.NativeMethods.SHEmptyRecycleBinFlags.SHERB_NOPROGRESSUI, |
|---|
| 347 | |
|---|
| 348 | /// <summary> |
|---|
| 349 | /// No sound will be played when the operation is complete. |
|---|
| 350 | /// </summary> |
|---|
| 351 | NoSound = (int)ShellApi.NativeMethods.SHEmptyRecycleBinFlags.SHERB_NOSOUND |
|---|
| 352 | } |
|---|
| 353 | } |
|---|