在C#中将文件删除到Windows x64回收站

5

我有一个类,在非64位系统上似乎运行得很好。

using System;
using System.Runtime.InteropServices;

namespace DeleteToRecycleBin
{
/// <summary>
/// Send files directly to the recycle bin.
/// </summary>
public class RecybleBin
{

    /// <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, Pack = 1)]
    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 readonly bool fAnyOperationsAborted;
        public readonly IntPtr hNameMappings;
        public readonly 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
        {
            SHFILEOPSTRUCT fs = new SHFILEOPSTRUCT
                                    {
                                        wFunc = FileOperationType.FO_DELETE,
                                        pFrom = path + '\0' + '\0',
                                        fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags
                                    };

            // important to double-terminate the string.
            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 SendSilent(string path)
    {
        return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI | FileOperationFlags.FOF_SILENT);

    }
}

有没有办法修复它,使其在x64上表现良好?我尝试了将ushort更改为ulong和其他几个修改,但都不起作用。

我知道有其他解决方案需要引用Microsoft.VisualBasic,但我更喜欢p/invoke方式。

正确的答案是:

在x64下,SHFILEOPSTRUCT必须声明为不带Pack = 1参数,否则将失败。如果您想使您的代码具有平台独立性,那么这真的很麻烦,因为您必须声明两个不同的结构体,一个带Pack = 1,另一个不带。然后,您必须声明两个不同的SHFileOperation调用,一个用于每个结构。然后,您必须根据您是否运行在32位或64位上来决定要调用哪一个。


如PInvoke网站所述: 不要为Pack大小声明一个值。如果省略它,在编组时将使用正确的值,并且可以为32位和64位操作使用单个SHFILEOPSTRUCT。 - Nir Kornfeld
2个回答

8
尽管看起来很奇怪,但.NET已经拥有将文件删除到回收站的功能......只是它们位于Microsoft.VisualBasic命名空间中。具体而言,是Microsoft.VisualBasic.FileIO
using Microsoft.VisualBasic.FileIO;

// class declaration, method start here

// Send file to recycle bin without dialog
FileSystem.DeleteFile("pathToFile", UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);

// Send file to recycle bin without dialog
FileSystem.DeleteFile("pathToFile", UIOption.AllDialogs, RecycleOption.SendToRecycleBin);

// Directories are the same, but with DeleteDirectory instead

我知道这个解决方案,但更喜欢不使用对VisualBasic的引用。 - MadBoy
@MadBoy 为什么你想避免引用 Microsoft.VisualBasic? - Nick
我没有完美的理由,只是个人偏好。如果我无法修复我已有的代码,我会使用Visual Basic参考,但我想应该可以修复它。这可能是int32与int64之间的某些差异或类似问题。 - MadBoy
请注意,此方法不适用于非 UI 交互式应用程序,例如 Windows 服务。请参阅 [MSDN],其中指定了重启选项仅适用于交互式应用程序。 - rboy

2
你看过PInvoke网站了吗?它对于FILEOPSTRUCT类型有略微不同的定义,其中强制使用Unicode。我想那个charset = auto可能会引起混淆……例如在32位系统上默认使用ANSI字符集,而在64位系统上使用Unicode字符集,所以在中间某个地方可能会出错。
编辑: 此外,Visual Basic参考方法很简单……我知道人们因为某些原因对它有偏见,但相关的DLL仍然是核心框架的一部分,因此不会添加任何新的依赖项。

问题出在 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)] 这里。对于64位系统,您需要删除Pack = 1,这样就可以解决问题了!由于您指向了PInvoke网站,它甚至在文章底部说到了这一点,即当操作系统为64位时,请删除Pack = 1! - MadBoy
哈哈,我甚至没有滚动那么远,我只看到了主要结构定义中的差异。尽管如此,它似乎是一种解决方案,但有点笨拙。 - Gareth Wilson
我正在使用64位Windows 7,Pack = 1的FILEOPSTRUCT,并且它在我的计算机上运行良好,没有任何问题 :| - TheBlueSky
请注意,此方法不适用于非 UI 交互式应用程序,例如 Windows 服务。有关仅在活动会话中运行的 UNDO 标志的详细信息,请参阅 [MSDN] (https://msdn.microsoft.com/en-us/library/windows/desktop/bb759795%28v=vs.85%29.aspx)。 - rboy
如PInvoke网站所述: 不要为Pack大小声明一个值。如果省略它,在编组时会使用正确的值,并且可以为32位和64位操作使用单个SHFILEOPSTRUCT。 - Nir Kornfeld

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接