public static void Delete(string path, bool recursive) { path = Path.GetFullPath(path); Object record = FileSystemManager.LockDirectory(path); try { uint attributes = NativeIO.GetAttributes(path); if (attributes == 0xFFFFFFFF) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotFound); } if (((attributes & (uint)(FileAttributes.Directory)) == 0) || ((attributes & (uint)(FileAttributes.ReadOnly)) != 0)) { /// it's readonly or not a directory throw new IOException("", (int)IOException.IOExceptionErrorCode.UnauthorizedAccess); } if (!Exists(path)) // make sure it is indeed a directory (and not a file) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotFound); } if (!recursive) { NativeFindFile ff = new NativeFindFile(path, "*"); try { if (ff.GetNext() != null) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotEmpty); } } finally { ff.Close(); } } NativeIO.Delete(path); } finally { // regardless of what happened, we need to release the directory when we're done FileSystemManager.UnlockDirectory(record); } }