internal static void DeleteWithUndo(string path) { NativeMethods.SHFILEOPSTRUCT32 lpFileOp = new NativeMethods.SHFILEOPSTRUCT32(); lpFileOp.fFlags = (ushort)13396; lpFileOp.hNameMappings = IntPtr.Zero; try { lpFileOp.hwnd = Process.GetCurrentProcess().MainWindowHandle; } catch (Exception ex) { if (!(ex is SecurityException) && !(ex is InvalidOperationException) && !(ex is NotSupportedException)) { throw; } else { lpFileOp.hwnd = IntPtr.Zero; } } lpFileOp.lpszProgressTitle = string.Empty; lpFileOp.pFrom = path + "\0\0"; lpFileOp.pTo = (string)null; lpFileOp.wFunc = 3U; lpFileOp.fAnyOperationsAborted = false; bool flag = NativeMethods.FileExists(path); if (IntPtr.Size == 4) { NativeMethods.SHFileOperation32(ref lpFileOp); } else if (IntPtr.Size == 8) { NativeMethods.SHFileOperation64(ref new NativeMethods.SHFILEOPSTRUCT64() { fFlags = lpFileOp.fFlags, hNameMappings = lpFileOp.hNameMappings, hwnd = lpFileOp.hwnd, lpszProgressTitle = lpFileOp.lpszProgressTitle, pFrom = lpFileOp.pFrom, pTo = lpFileOp.pTo, wFunc = lpFileOp.wFunc, fAnyOperationsAborted = lpFileOp.fAnyOperationsAborted }); } if (flag) { NativeMethods.SHChangeNotify(4U, 5U, path, IntPtr.Zero); } else { NativeMethods.SHChangeNotify(16U, 5U, path, IntPtr.Zero); } }
private static extern int SHFileOperation32(ref NativeMethods.SHFILEOPSTRUCT32 lpFileOp);