Send a File to the Recycle Bin

Send a File to the Recycle Bin

NOTE: This also does not work with non UI Interactive apps like Windows Services

This wrapper can provide you needed functionality:

using System.Runtime.InteropServices;

public class FileOperationAPIWrapper
{
/// <summary>
/// Possible flags for the SHFileOperation method.
/// </summary>
[Flags]
public enum FileOperationFlags : ushort
{
/// <summary>
/// Do not show a dialog during the process
/// </summary>
FOF_SILENT = 0x0004,
/// <summary>
/// Do not ask the user to confirm selection
/// </summary>
FOF_NOCONFIRMATION = 0x0010,
/// <summary>
/// Delete the file to the recycle bin. (Required flag to send a file to the bin
/// </summary>
FOF_ALLOWUNDO = 0x0040,
/// <summary>
/// Do not show the names of the files or folders that are being recycled.
/// </summary>
FOF_SIMPLEPROGRESS = 0x0100,
/// <summary>
/// Surpress errors, if any occur during the process.
/// </summary>
FOF_NOERRORUI = 0x0400,
/// <summary>
/// Warn if files are too big to fit in the recycle bin and will need
/// to be deleted completely.
/// </summary>
FOF_WANTNUKEWARNING = 0x4000,
}

/// <summary>
/// File Operation Function Type for SHFileOperation
/// </summary>
public enum FileOperationType : uint
{
/// <summary>
/// Move the objects
/// </summary>
FO_MOVE = 0x0001,
/// <summary>
/// Copy the objects
/// </summary>
FO_COPY = 0x0002,
/// <summary>
/// Delete (or recycle) the objects
/// </summary>
FO_DELETE = 0x0003,
/// <summary>
/// Rename the object(s)
/// </summary>
FO_RENAME = 0x0004,
}

/// <summary>
/// SHFILEOPSTRUCT for SHFileOperation from COM
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct SHFILEOPSTRUCT
{

public IntPtr hwnd;
[MarshalAs(UnmanagedType.U4)]
public FileOperationType wFunc;
public string pFrom;
public string pTo;
public FileOperationFlags fFlags;
[MarshalAs(UnmanagedType.Bool)]
public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}

[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);

/// <summary>
/// Send file to recycle bin
/// </summary>
/// <param name="path">Location of directory or file to recycle</param>
/// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param>
public static bool Send(string path, FileOperationFlags flags)
{
try
{
var fs = new SHFILEOPSTRUCT
{
wFunc = FileOperationType.FO_DELETE,
pFrom = path + '\0' + '\0',
fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags
};
SHFileOperation(ref fs);
return true;
}
catch (Exception)
{
return false;
}
}

/// <summary>
/// Send file to recycle bin. Display dialog, display warning if files are too big to fit (FOF_WANTNUKEWARNING)
/// </summary>
/// <param name="path">Location of directory or file to recycle</param>
public static bool Send(string path)
{
return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_WANTNUKEWARNING);
}

/// <summary>
/// Send file silently to recycle bin. Surpress dialog, surpress errors, delete if too large.
/// </summary>
/// <param name="path">Location of directory or file to recycle</param>
public static bool MoveToRecycleBin(string path)
{
return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI | FileOperationFlags.FOF_SILENT);

}

private static bool deleteFile(string path, FileOperationFlags flags)
{
try
{
var fs = new SHFILEOPSTRUCT
{
wFunc = FileOperationType.FO_DELETE,
pFrom = path + '\0' + '\0',
fFlags = flags
};
SHFileOperation(ref fs);
return true;
}
catch (Exception)
{
return false;
}
}

public static bool DeleteCompletelySilent(string path)
{
return deleteFile(path,
FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI |
FileOperationFlags.FOF_SILENT);
}
}

Moving file to Recycle Bin

To send something to the recycle bin it is much easier to use send2trash. It is cross platform and very easy to use.

You can install it with :

pip install Send2Trash

Then you can use it :

from send2trash import send2trash
send2trash(filename)

How to send a file to the recycle bin in Unity3D

Send a File to the Recycle Bin is indeed the way to go. I found that simply

using System;

was missing from my attempts to use the FileOperationAPIWrapper script. I've since been successfully moving files to the recycle bin using it.

System.IO.Directory.Delete Method just sends files to Recycling Bin. Any way to permanently delete?

Have a look at MSDN:FileSystem.DeleteDirectory.

It has the parameter RecycleOption recycle which uses the enum with the following options:

  • DeletePermanently
  • SendToRecycleBin

So calling the following would be sufficient:

My.Computer.FileSystem.DeleteDirectory(
DirectoryPath,
FileIO.UIOption.AllDialogs,
FileIO.RecycleOption.DeletePermanently);

How do you place a file in recycle bin instead of delete?

http://www.daveamenta.com/2008-05/c-delete-a-file-to-the-recycle-bin/

From above:

using Microsoft.VisualBasic;

string path = @"c:\myfile.txt";
FileIO.FileSystem.DeleteDirectory(path,
FileIO.UIOption.OnlyErrorDialogs,
RecycleOption.SendToRecycleBin);

Can node.js send a file / folder to recycle bin (trash on MacOS) instead of fs.unlink / fs.rmdir?

Node.js's fs doesn't provide any api for moving files to Trash/Recycle Bin, but you can try trash node-module to have work-around.

Windows Recycle Bin information file binary format

Okay, I got it.

While
00 00 00 00 00 00 00 00 is 1899.12.30 00:00,

but 00 00 00 00 00 00 01 00 is 1601.11.22 18:44,

so this timestamp is a number of 100-nanosecond intervals since 1601.01.01.

For example, for 00 00 00 00 00 29 d7 01 (132619794007457792) I get the correct date (2021.04.04 03:10) as it shows in File Explorer with:

new Date(132619794007457792 / 10000 + Number(new Date("1601.01.01 Z")))

Anyway, I think this topic would be useful for people.

It's strange that in 2021 I did not found any info about how Recycle Bin in Windows works.



Related Topics



Leave a reply



Submit