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
    }
}
相关推荐
andyweike2 小时前
php笔记
开发语言·笔记·php
传奇开心果编程3 小时前
【SwiftUI娓娓道来】第3课:让界面活起来——交互与动画指南
学习·macos·ui·ios·swiftui·swift
Circ.3 小时前
Python 调试 Litellm 本地大模型接口:模型列表与对话接口实操踩坑记录
开发语言·python
zhchyun20084 小时前
【Unity UI 进阶】仿 Element UI 打造企业级 Unity UI 组件库(10)
ui·unity·游戏引擎
传奇开心果编程4 小时前
【Jetpack Compose进阶学与练】第14课:系列收尾复习总结;Compose项目常见坑点汇总;学习路线与后续学习方向
android·学习·ui·kotlin·android jetpack
波力海苔夹心脆6754 小时前
C# 序列化与反序列化详解:System.Text.Json、Newtonsoft.Json、XmlSerializer 用法、特性选项与安全实践
经验分享·后端·c#·json·.net
现任明教教主~5 小时前
Thinkphp站群蜘蛛池SaaS系统YanyvSEO含多用户/积分/六大引擎计费/易支付对接
java·开发语言·spring
Zhou1411365 小时前
SpringSecurity_02_授权与高级功能
开发语言·python
码匠许师傅6 小时前
【C++三方组件】cpp-httplib:一个头文件起 HTTP 服务
开发语言·c++·http