tomWindowsRecycle function

bool tomWindowsRecycle(
  1. String absolute
)

Moves absolute — a file or a folder, a full Windows path — to the Recycle Bin. Answers whether it went there; nothing is asked of the user and no dialog is shown.

Implementation

bool tomWindowsRecycle(String absolute) {
  final shell32 = DynamicLibrary.open('shell32.dll');
  final operate = shell32
      .lookupFunction<_ShFileOperationNative, _ShFileOperation>(
        'SHFileOperationW',
      );
  final units = absolute.replaceAll('/', r'\').codeUnits;
  // pFrom is a list of paths, each NUL-terminated, the list ending in a
  // second NUL.
  final from = calloc<Uint16>(units.length + 2);
  final op = calloc<_ShFileOpStruct>();
  try {
    for (var i = 0; i < units.length; i++) {
      from[i] = units[i];
    }
    op.ref
      ..wFunc = _foDelete
      ..pFrom = from
      ..fFlags =
          _fofAllowUndo | _fofNoConfirmation | _fofSilent | _fofNoErrorUi;
    final result = operate(op);
    return result == 0 && op.ref.fAnyOperationsAborted == 0;
  } finally {
    calloc.free(from);
    calloc.free(op);
  }
}