最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)刪除文件和目錄到回收站

 更新時(shí)間:2025年01月21日 10:51:31   作者:秋月的私語(yǔ)  
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)刪除文件和目錄到回收站,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

之前在c++上遇到過(guò)這個(gè)問(wèn)題,折騰許久才解決了,這次在c#上再次遇到這個(gè)問(wèn)題,不過(guò)似乎容易了一些,親測(cè)代碼如下,兩種刪除方式都寫在代碼中了。

直接上完整代碼:

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($"無(wú)法將文件/目錄 {path} 移動(dòng)到回收站: {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
    }
}

到此這篇關(guān)于C#實(shí)現(xiàn)刪除文件和目錄到回收站的文章就介紹到這了,更多相關(guān)C#刪除文件和目錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

滦南县| 伊春市| 万州区| 上高县| 连云港市| 兴文县| 吉林市| 商城县| 波密县| 宁蒗| 青冈县| 浏阳市| 长宁县| 松溪县| 高唐县| 阳谷县| 乌海市| 航空| 静海县| 白山市| 浦江县| 昌邑市| 吉林市| 寿阳县| 梅河口市| 蒙阴县| 龙井市| 长武县| 南雄市| 蚌埠市| 和顺县| 英德市| 建宁县| 阜新市| 怀柔区| 平谷区| 招远市| 车险| 清徐县| 陕西省| 渭源县|