最新国产好看的视频,伊人天堂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)論

台州市| 尉氏县| 孝昌县| 东兰县| 海口市| 安阳县| 伊宁市| 夏邑县| 五峰| 老河口市| 株洲县| 女性| 桐乡市| 察隅县| 西峡县| 蒲城县| 凤台县| 甘洛县| 太保市| 安徽省| 孟州市| 平南县| 富源县| 北川| 肃宁县| 吉安县| 巧家县| 罗甸县| 丽江市| 柳江县| 青川县| 夹江县| 许昌县| 上栗县| 通化市| 保德县| 平顺县| 深水埗区| 乐都县| 彭山县| 揭东县|