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
    }
}
相关推荐
matlab代码15 分钟前
基于matlab肤色人脸标记检测系统 GUI界面【源码53期】
开发语言·matlab·人脸标记
KhalilRuan25 分钟前
UnityCsReference——笔记
java·开发语言·笔记
电子云与长程纠缠28 分钟前
UE5 Lyra PocketWorld进行3D内容UI预览 - 上
开发语言·学习·3d·ue5·游戏引擎
码农大叔的博客38 分钟前
golang示例:for九九乘法表
开发语言·算法·golang
长谷深风1111 小时前
Agent 何时该 Replan:五个关键判断
java·大数据·开发语言·ai agent·ai智能体·agent设计·clarify机制
lingran__1 小时前
C++ STL unordered系列(哈希) 底层剖析与模拟实现万字详解 | 基于哈希表,复刻 SGI-STL 泛型哈希容器架构
开发语言·c++·后端·哈希算法·哈希表·泛型编程·unordered系列
萧瑟余晖2 小时前
Java深入解析篇三十四之分布式事务
java·开发语言·分布式
CAE虚拟与现实2 小时前
源码注解 /class 注解 / 运行时注解三者生命周期
java·开发语言
有脚就行2 小时前
第24篇-Go-gRPC推理服务-高性能跨语言通信
开发语言·人工智能·后端·golang
码匠许师傅2 小时前
【C++ 面试真题】聊聊 C++ 的关联容器
开发语言·c++·面试