亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

File.Delete 上的 System.IO.DirectoryNotFound

File.Delete 上的 System.IO.DirectoryNotFound

C#
www說 2023-09-16 17:05:35
我編寫了代碼來遞歸刪除文件夾及其所有結構。我從 獲得了System.IO.DirectoryNotFoundException一個File.Delete有效的文件名Directory.GetFiles(path)。我File.Exists之前添加過,它告訴我該文件不存在,我也可以在資源管理器中看到它。我知道我可以做到,SHFileOperation應該可以正常工作。但我想使用本機 C#(沒有直接的 Windows API)。我在 Windows 10 上紅色:.NET 4.6.2 和長路徑,并在本地組策略編輯器中設置啟用“啟用 Win 32 長路徑”。我正在使用 .NET Framework 4.7.2。有誰能告訴我我的代碼有什么問題嗎?或者我能做些什么來讓它發揮作用?錯誤的路徑是:'E:\CobianBackupOld\cn1629\AppData\Local\Packages\Microsoft.Windows.Cortana_cw5n1h2txyewy\LocalState\AppIconCache\100\{7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}_Microsoft Visual Studio 9_0_Application_PreEmptive Solutions_Dotfuscator Community Edition_dotfuscator _exe'我是該文件的所有者(我有權限刪除它)。
查看完整描述

1 回答

?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

雖然沒有我希望的那么完美,但我通過使用前綴“\?\”并確保在刪除之前將屬性設置為正常來修復了我的錯誤。


我還是不明白為什么我還必須這樣做???為什么它在最近的.net庫中仍然沒有修復?


最終代碼:


using System;

using System.Diagnostics;

using System.IO;


namespace HQ.Util.General.IO

{

    public class DirectoryRemoverRecursive

    {

        private readonly Action<string, string, int> _pathStatus;

        private readonly Func<string, bool> _callbackCanRemoveFile;

        private readonly Func<string, bool> _callbackCanRemoveFolder;

        private Func<bool> _shouldCancel;


        public int Count { get; private set; } = 0;


        /// <summary>

        /// 

        /// </summary>

        /// <param name="pathStatus">Arguments are [path] and [null on success or exception message]</param>

        /// <param name="callbackCanRemoveFile">Argument is path and should return true to delete. If this function is null, all path will be deleted.</param>

        /// <param name="callbackCanRemoveFolder">Argument is path and should return true to delete. If this function is null, all path will be deleted.</param>

        /// <param name="shouldCancel">If null will never cancel. Cancel when func return true</param>

        public DirectoryRemoverRecursive(

            Action<string, string, int> pathStatus = null, 

            Func<string, bool> callbackCanRemoveFile = null, 

            Func<string, bool> callbackCanRemoveFolder = null, 

            Func<bool> shouldCancel = null)

        {

            _pathStatus = pathStatus;

            _callbackCanRemoveFile = callbackCanRemoveFile;

            _callbackCanRemoveFolder = callbackCanRemoveFolder;

            _shouldCancel = shouldCancel;

        }


        // ******************************************************************

        /// <summary>

        /// return true if canceled

        /// </summary>

        /// <param name="path"></param>

        /// <returns></returns>

        public bool Remove(string path)

        {

            string result = null;


            if (path.Length < 2 || !path.StartsWith(@"\\"))

            {

                path = @"\\?\" + path;

            }


            if (Directory.Exists(path))

            {

                foreach (var subDir in Directory.GetDirectories(path))

                {

                    if (_shouldCancel != null)

                    {

                        if (_shouldCancel())

                        {

                            return true;

                        }

                    }


                    if (Remove(subDir))

                    {

                        return true;

                    }

                }


                foreach (var filename in Directory.GetFiles(path))

                {

                    if (_shouldCancel != null)

                    {

                        if (_shouldCancel())

                        {

                            return true;

                        }

                    }


                    if (Remove(filename))

                    {

                        return true;

                    }

                }


                try

                {

                    if (_callbackCanRemoveFolder != null)

                    {

                        if (!_callbackCanRemoveFolder(path))

                        {

                            return false;

                        }

                    }


                    Directory.Delete(path);

                    Count++;

                    result = null;

                }

                catch (Exception ex)

                {

                    try

                    {

                        File.SetAttributes(path, FileAttributes.Normal);

                        Directory.Delete(path);

                        Count++;

                        result = null;

                    }

                    catch (Exception)

                    {

                        result = "Try to delete directory exception: " + ex.ToString();

                    }

                }

            }

            else

            {

                try

                {

                    if (File.Exists(path))

                    {

                        if (_callbackCanRemoveFile != null)

                        {

                            if (!_callbackCanRemoveFile(path))

                            {

                                return false;

                            }

                        }


                        File.Delete(path);

                        Count++;

                        result = null;

                    }

                    else

                    {

                        Debug.Print($"File does not exists {path}");

                    }

                }

                catch (Exception ex)

                {

                    try

                    {

                        File.SetAttributes(path, FileAttributes.Normal);

                        File.Delete(path);

                        Count++;

                        result = null;

                    }

                    catch (Exception)

                    {

                        result = "Try to delete file exception: " + ex.ToString();

                    }

                }

            }


            _pathStatus?.Invoke(path, result, Count);


            return false;

        }


        // ******************************************************************


    }


}



查看完整回答
反對 回復 2023-09-16
  • 1 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號