C#.NET 删除文件到回收站

导入命名空间   using System.Runtime.InteropServices;

 在类中定义方法属性:

  public class MessageBox
{
      /// <summary>
    /// 删除文件到回收站
    /// </summary>
    /// <param name="path">删除文件的路径</param>
    /// <returns></returns>
     public static int DeleteFileToRecycleBin(string path)
    {
        _SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT();
        pm.wFunc = FO_DELETE;
        pm.pFrom = path + '/0';
        pm.pTo = null;
        pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
        return SHFileOperation(pm);
    }
 

    private const int FO_DELETE = 0x3;
    private const ushort FOF_NOCONFIRMATION = 0x10;
    private const ushort FOF_ALLOWUNDO = 0x40;

  
    [DllImport("shell32.dll")]
    private static extern int SHFileOperation([In, Out] _SHFILEOPSTRUCT str);
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]

public class _SHFILEOPSTRUCT
{
    public IntPtr hwnd;
    public UInt32 wFunc;
    public string pFrom;
    public string pTo;
    public UInt16 fFlags;
    public Int32 fAnyOperationsAborted;
    public IntPtr hNameMappings;
    public string lpszProgressTitle;
}

你可能感兴趣的:(String,shell,null,delete,Class,Path)