Send a file to the recycle bin in C#
ctg | 26 Mayo, 2006 00:02using System;
using System.IO;
using System.Runtime.InteropServices;
/// <summary>
/// Class to delete a file and move it to Recycle bin
/// </summary>
class RecycleBin
{
private const int FO_DELETE = 3;
private const int FOF_ALLOWUNDO = 0x40;
private const int FOF_NOCONFIRMATION = 0x0010;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
[MarshalAs(UnmanagedType.U4)] public int wFunc;
public string pFrom;
public string pTo;
public short fFlags;
[MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}
[DllImport("shell32.dll", CharSet=CharSet.Auto)]
static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
/// <summary>
/// Delete a file and move it to Recycle bin
/// </summary>
/// <param name="fileName">File to delete</param>
public static void Send(string fileName)
{
if (File.Exists(fileName) )
{
SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.hwnd = IntPtr.Zero;
shf.wFunc = (int) FO_DELETE;
shf.pTo = null;
shf.pFrom = fileName + "\0\0";
shf.fFlags = (ushort) FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
shf.fAnyOperationsAborted = false;
shf.hNameMappings = IntPtr.Zero;
SHFileOperation(ref shf);
}
}
}
Posted in
C# .
Comentario: (0).
Retroenlaces:(0).
Enlace
«Next post |
Previous post»