public static bool RecycleFile(string FullFilePath) { if (!System.IO.File.Exists(FullFilePath)) { return(true); } IntPtr FilePathPtr = IntPtr.Zero; try { var FullFilePathPtrValue = FullFilePath + Convert.ToChar(0) + Convert.ToChar(0); FilePathPtr = Marshal.StringToHGlobalAuto(FullFilePathPtrValue); if (Is64BitProcess) { var Operation = new SHFILEOPSTRUCTx64(); Operation.wFunc = FO_Func.FO_DELETE; Operation.pFrom = FilePathPtr; Operation.fFlags = FO_FileOp.FOF_ALLOWUNDO | FO_FileOp.FOF_NOCONFIRMATION | FO_FileOp.FOF_SILENT | FO_FileOp.FOF_NOERRORUI; int Result = NativeMethodsx64.SHFileOperation(ref Operation); return(Result == 0); } else { var Operation = new SHFILEOPSTRUCTx86(); Operation.wFunc = FO_Func.FO_DELETE; Operation.pFrom = FilePathPtr; Operation.fFlags = FO_FileOp.FOF_ALLOWUNDO | FO_FileOp.FOF_NOCONFIRMATION | FO_FileOp.FOF_SILENT | FO_FileOp.FOF_NOERRORUI; int Result = NativeMethods.SHFileOperation(ref Operation); return(Result == 0); } } finally { if (FilePathPtr != IntPtr.Zero) { Marshal.Release(FilePathPtr); } } }
public static extern int SHFileOperation(ref SHFILEOPSTRUCTx86 FileOp);