默认删除文件的时候 File.Delete 是将文件永久删除,如果是一些文档,建议删除到回收站,这样用户可以自己还原
通过 SHFileOperation 可以将文件放在回收站

本文提供的方法暂时只能在 x86 程序使用,此方法暂时不适合 dotnet core 程序

添加一个帮助类

    public static class RecycleBin
    {
        public static void DeleteToRecycleBin(string file)
        {
            var shf = new SHFILEOPSTRUCT
            {
                wFunc = FO_DELETE,
                fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION,
                // pFrom 需要在字符串后面加两个 \0 才可以 https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/ns-shellapi-_shfileopstructa
                pFrom = file + "\0"
            };
            SHFileOperation(ref shf);
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
        private struct SHFILEOPSTRUCT
        {
            public int hwnd;
            [MarshalAs(UnmanagedType.U4)] public int wFunc;
            public string pFrom;
            public string pTo;
            public short fFlags;
            [MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted;
            public int hNameMappings;
            public string lpszProgressTitle;
        }

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

        private const int FO_DELETE = 3;
        private const int FOF_ALLOWUNDO = 0x40;
        private const int FOF_NOCONFIRMATION = 0x10;
    }

这个类里面只有一个公开方法,要求传入一个文件

下面添加一些测试的代码

        static void Main(string[] args)
        {
            var folder = @"D:\lindexi\github\";
            var file = Path.Combine(folder, "欢迎访问我博客 blog.lindexi.com 里面有大量 UWP WPF 博客.txt");
            Directory.CreateDirectory(folder);
            if (!File.Exists(file))
            {
                File.WriteAllText(file, "欢迎访问我博客 blog.lindexi.com 里面有大量 UWP WPF 博客");
            }

            RecycleBin.DeleteToRecycleBin(file);
        }

尝试运行代码

dotnet 将文件删除到回收站_dotnet

如果可以引用Microsoft.VisualBasic那么写起来就很清真

Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(filename or folder,
      Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, 
        Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);

代码在github

c# - Send a File to the Recycle Bin - Stack Overflow

Delete files or folders to recycle bin in C#