問題:
I'm using .NET 3.5, trying to recursively delete a directory using:我正在使用 .NET 3.5,嘗試使用以下方法遞歸刪除目錄:
Directory.Delete(myPath, true);
My understanding is that this should throw if files are in use or there is a permissions problem, but otherwise it should delete the directory and all of its contents.我的理解是,如果文件正在使用或存在權限問題,這應該拋出,否則它應該刪除目錄及其所有內容。
However, I occasionally get this:但是,我偶爾會得到這個:
System.IO.IOException: The directory is not empty.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)
at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)
...
I'm not surprised that the method sometimes throws, but I'm surprised to get this particular message when recursive is true.我對這個方法有時會拋出並不感到驚訝,但是當 recursive 爲 true 時,我很驚訝收到這個特定的消息。 (I know the directory is not empty.) (我知道目錄不是空的。)
Is there a reason I'd see this instead of AccessViolationException?有什麼理由讓我看到這個而不是 AccessViolationException?
解決方案:
參考一: https://stackoom.com/question/1NgB參考二: Cannot delete directory with Directory.Delete(path, true)