SHFileOperation (shell32)
Last changed: Quaint Mako-88.174.61.36

.
Summary

C# Signature:

[DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true, ThrowOnUnmappableChar = true)]
static extern int SHFileOperation(ref SHFILEOPSTRUCT lpFileOp);

VB.NET Signature:

<DllImport("shell32.dll", CharSet:=CharSet.Auto, SetLastError:=true, ThrowOnUnmappableChar:=true)> _
Public Function SHFileOperation(<MarshalAs(UnmanagedType.Struct)>ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
End function

VB Signature

Public Declare Function SHFileOperation Lib "shell32" Alias "SHFileOperationA" _
        (lpFileOp As SHFILEOPSTRUCT) As Long

User-Defined Types:

SHFILEOPSTRUCT, FileFuncFlags, FILEOP_FLAGS

Notes:

Do not use GetLastError with the return values of this function.

It's important that the layout of the SHFILEOPSTRUCT structure differs for 32-bit processes and 64-bit processes.

pTo and pFrom must be end on two \0 characters. As C# strings automatically end one implicit \0 character, another must be added to the end.

Tips & Tricks:

As the struct layout differs between 32-bit and 64-bit processes the struct and method should be defined twice. After checking the current processes bitness call the appropriate version. This is the same approach used by Microsoft in Microsoft.VisualBasic.dll for the FileIO.FileSystem.DeleteFile implementation.

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
public struct SHFILEOPSTRUCT32
{
   public IntPtr hwnd;
   public uint wFunc;
   [MarshalAs(UnmanagedType.LPTStr)]
   public string pFrom;
   [MarshalAs(UnmanagedType.LPTStr)]
   public string pTo;
   public ushort fFlags;
   public bool fAnyOperationsAborted;
   public IntPtr hNameMappings;
   [MarshalAs(UnmanagedType.LPTStr)]
   public string lpszProgressTitle;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SHFILEOPSTRUCT64
{
   public IntPtr hwnd;
   public uint wFunc;
   [MarshalAs(UnmanagedType.LPTStr)]
   public string pFrom;
   [MarshalAs(UnmanagedType.LPTStr)]
   public string pTo;
   public ushort fFlags;
   public bool fAnyOperationsAborted;
   public IntPtr hNameMappings;
   [MarshalAs(UnmanagedType.LPTStr)]
   public string lpszProgressTitle;
}

[DllImport("shell32.dll", EntryPoint = "SHFileOperation", CharSet = CharSet.Auto, SetLastError = true, ThrowOnUnmappableChar = true)]
static extern int SHFileOperation32(ref SHFILEOPSTRUCT32 lpFileOp);

[DllImport("shell32.dll", EntryPoint = "SHFileOperation", CharSet = CharSet.Auto, SetLastError = true, ThrowOnUnmappableChar = true)]
static extern int SHFileOperation64(ref SHFILEOPSTRUCT64 lpFileOp);

public unsafe void SendToRecycleBin(string path, bool silent = true)
{
   var flags = FILEOP_FLAGS.FOF_ALLOWUNDO;
   if (silent) { flags |= FILEOP_FLAGS.FOF_SILENT | FILEOP_FLAGS.FOF_NOCONFIRMATION; }

   if (sizeof(IntPtr) == 4)
   {
     var data = new SHFILEOPSTRUCT32
     {
       wFunc = (uint)FileFuncFlags.FO_DELETE,
       pFrom = path + '\0',
       fFlags = (ushort)flags
     };
     SHFileOperation32(ref data);
   }
   else
   {
     var data = new SHFILEOPSTRUCT64
     {
       wFunc = (uint)FileFuncFlags.FO_DELETE,
       pFrom = path + '\0',
       fFlags = (ushort)flags
     };
     SHFileOperation64(ref data);
   }
}

Alternative Managed API:

Microsoft.VisualBasic.dll/Nuget package:

Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(string, Microsoft.VisualBasic.FileIO.UIOption, Microsoft.VisualBasic.FileIO.RecycleOption);

Documentation