Currently I\'m using the following function
file.Delete();
But how can I use this function to send a file to the recycle bin instead of jus
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
{
///
/// Possible flags for the SHFileOperation method.
///
[Flags]
public enum FileOperationFlags : ushort
{
///
/// Do not show a dialog during the process
///
FOF_SILENT = 0x0004,
///
/// Do not ask the user to confirm selection
///
FOF_NOCONFIRMATION = 0x0010,
///
/// Delete the file to the recycle bin. (Required flag to send a file to the bin
///
FOF_ALLOWUNDO = 0x0040,
///
/// Do not show the names of the files or folders that are being recycled.
///
FOF_SIMPLEPROGRESS = 0x0100,
///
/// Surpress errors, if any occur during the process.
///
FOF_NOERRORUI = 0x0400,
///
/// Warn if files are too big to fit in the recycle bin and will need
/// to be deleted completely.
///
FOF_WANTNUKEWARNING = 0x4000,
}
///
/// File Operation Function Type for SHFileOperation
///
public enum FileOperationType : uint
{
///
/// Move the objects
///
FO_MOVE = 0x0001,
///
/// Copy the objects
///
FO_COPY = 0x0002,
///
/// Delete (or recycle) the objects
///
FO_DELETE = 0x0003,
///
/// Rename the object(s)
///
FO_RENAME = 0x0004,
}
///
/// SHFILEOPSTRUCT for SHFileOperation from COM
///
[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);
///
/// Send file to recycle bin
///
/// Location of directory or file to recycle
/// FileOperationFlags to add in addition to FOF_ALLOWUNDO
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;
}
}
///
/// Send file to recycle bin. Display dialog, display warning if files are too big to fit (FOF_WANTNUKEWARNING)
///
/// Location of directory or file to recycle
public static bool Send(string path)
{
return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_WANTNUKEWARNING);
}
///
/// Send file silently to recycle bin. Surpress dialog, surpress errors, delete if too large.
///
/// Location of directory or file to recycle
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);
}
}