c#删除文件和目录到回收站

之前在c++上遇到过这个问题,折腾许久才解决了,这次在c#上再次遇到这个问题,不过似乎容易了一些,亲测代码如下,两种删除方式都写在代码中了。

直接上完整代码:

cs 复制代码
using Microsoft.VisualBasic.FileIO;
using System;
using System.IO;
using System.Runtime.InteropServices;

namespace ceshiConsole
{
    public class FileIOHelper
    {
        [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;
        }

        #region Dllimport
        [DllImport("shell32.dll", CharSet = CharSet.Auto)]
        public static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
        #endregion
        #region Const
        public const int FO_DELETE = 3;
        public const int FOF_ALLOWUNDO = 0x40;
        public const int FOF_NOCONFIRMATION = 0x10;
        #endregion

        #region Public Static Method
        public static void DeleteFileToRecyclebin(string file, Boolean showConfirmDialog = false)
        {
            SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
            shf.wFunc = FO_DELETE;
            shf.fFlags = FOF_ALLOWUNDO;
            if (!showConfirmDialog)
            {
                shf.fFlags |= FOF_NOCONFIRMATION;
            }
            shf.pFrom = file + '\0' + '\0';
            SHFileOperation(ref shf);
        }

        public static bool SendToRecycleBin(string path)
        {
            bool bRet = true;
            try
            {
                if (File.Exists(path))
                {
                    FileSystem.DeleteFile(path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                }
                else if (Directory.Exists(path))
                {
                    FileSystem.DeleteDirectory(path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                }
                else
                {
                    bRet = false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"无法将文件/目录 {path} 移动到回收站: {ex.Message}");
                bRet = false;
            }

            return bRet;
        }

        static void Main(string[] args)
        {
            DeleteFileToRecyclebin(@"C:\Users\autumoon\Desktop\test.txt");
            SendToRecycleBin(@"C:\Users\autumoon\Desktop\test2.txt");
        }

        #endregion
    }
}
相关推荐
李匠20248 分钟前
C++GO语言微服务基础技术②
开发语言·c++·微服务·golang
BUG制造机.15 分钟前
Go 语言 slice(切片) 的使用
开发语言·后端·golang
柴薪之王、睥睨众生18 分钟前
(自用)Java学习-5.12(Redis,B2C电商)
java·开发语言·学习
geovindu24 分钟前
vue3: pdf.js 3.4.120 using javascript
开发语言·javascript·vue.js·pdf
编程乐趣28 分钟前
一个用C#开发的记事本Notepads开源编辑器
c#·编辑器·.net
编程乐趣34 分钟前
点下4个Winform UI开源控件库
ui·开源·mfc
Cuit小唐39 分钟前
C++ 状态模式详解
开发语言·c++·状态模式
落羽的落羽1 小时前
【落羽的落羽 C++】stack和queue、deque、priority_queue、仿函数
开发语言·c++
阿乾之铭1 小时前
Java后端文件类型检测(防伪造)
java·开发语言
sunbyte1 小时前
Three.js + React 实战系列 - 联系方式提交表单区域 Contact 组件✨(表单绑定 + 表单验证)
开发语言·javascript·react.js