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
    }
}
相关推荐
孟紫瑶3 分钟前
C#语言的游戏引擎
开发语言·后端·golang
{⌐■_■}21 分钟前
【go】slice的浅拷贝和深拷贝
开发语言·后端·golang
闪电麦坤9528 分钟前
C#:is关键字
开发语言·c#
kovlistudio1 小时前
红宝书第三十一讲:通俗易懂的包管理器指南:npm 与 Yarn
开发语言·前端·javascript·学习·npm·node.js
搞不懂语言的程序员1 小时前
模板方法模式详解
java·开发语言·模板方法模式
码界筑梦坊2 小时前
基于Python的招聘推荐数据可视化分析系统
开发语言·爬虫·python·信息可视化·数据分析
CptainLee2 小时前
python小整数池和字符串贮存
java·开发语言·python
nlog3n2 小时前
Java访问者模式详解
java·开发语言·访问者模式
牛马大师兄2 小时前
Shell脚本编程之正则表达式
linux·运维·服务器·开发语言·ssh·bash·shell
froginwe112 小时前
正则表达式 - 简介
开发语言