public static void DeleteDirectory(string folderPath, bool recursive) { folderPath = CheckAddLongPathPrefix(folderPath); if (DirectoryExists(folderPath)) { if (recursive) { var files = GetFiles(folderPath); var dirs = GetDirectories(folderPath); foreach (var file in files) { DeleteFile(file.FullName); } foreach (var dir in dirs) { DeleteDirectory(dir.FullName, true); } } if (!PInvokeHelper.RemoveDirectory(folderPath)) { { // http://msdn.microsoft.com/en-us/library/ms681382(VS.85).aspx. var lastWin32Error = Marshal.GetLastWin32Error(); if (lastWin32Error != PInvokeHelper.ERROR_NO_MORE_FILES) { throw new Win32Exception( lastWin32Error, string.Format( Resources.ErrorDeletingFolder, lastWin32Error, folderPath, CheckAddDotEnd(new Win32Exception(lastWin32Error).Message))); } } } } }