System.IO.Directory.Delete引發「目錄不是空的」的錯誤

System.IO.Directory.Delete方法從.NET Framework一開始就始終存在,近日發現某系統程式碼上面運行到這一行拋出目錄不是空的(The directory is not empty),在確認不存在任何系統級的咬檔、權限錯誤等問題後,再去翻閱一下MSDN發現誤會大了:Directory.Delete Method

System.IO.Directory.Delete只會刪除空白的目錄

以下是錯誤的相關訊息:

System.IO.IOException: '目錄不是空的。

此例外狀況原先在此呼叫堆疊擲回: 
    System.IO.__Error.WinIOError(int, string)
    System.IO.Directory.DeleteHelper(string, string, bool, bool, ref Microsoft.Win32.Win32Native.WIN32_FIND_DATA)
    System.IO.Directory.Delete(string, string, bool, bool)
    ASP.ap_independentenroll_webservices_ajaxsystemsetting_aspx.ClearData(out string) 位於 YourCode.cs

會引爆這行錯誤是程式設計師透過VisualStudio IntelliSense支援後,直覺的認為Delete方法就是將目錄刪除掉,這樣的認知在語意上、語境上其實是沒錯的,但.NET Framework提供的方法解釋卻是只刪除指定的空目錄,猜測大型框架的類別方法初始參數偏向謹慎之設計,應該也是為了遭到誤用的安全考量:

Delete(String)	
Deletes an empty directory from a specified path.

其實要正確不顧一切刪除某目錄必須採用另外一種多載方法(Method Overloading),透過遞迴參數(parameters: recursive)的指派,由程式設計師自己指定確定要刪除具備子目錄與檔案的目錄:

public static void Delete (string path, bool recursive);
//Deletes the specified directory and, if indicated, any subdirectories and files in the directory.

知道這個結果,我的下一步思路是「糟糕,那以前其他的地方用到這個API的方法呼叫,豈不是全部都錯了?」,後來清查程式碼後發現:並沒有,只有這一支程式碼是錯誤的,其他的都有正確的調用recursive參數(arguments: recursive)。

感想

只能說寫程式不要太相信自己的直覺,進行系統級還API操作,有時是得翻閱一下官方文件再確認一下會比較好。

相關連結

System.IO.Directory.Delete TheDirectoryIsNotEmpty TryCatch Error Recursive SubDirectories SubFiles