Recursive directory deletion that handles directories being opened in Windows Explorer. Source: http:stackoverflow.coma1703799 - CSharp File IO

CSharp examples for File IO:Directory

Description

Recursive directory deletion that handles directories being opened in Windows Explorer. Source: http:stackoverflow.coma1703799

Demo Code


using System.IO;//from w  w w. j  av  a  2 s  . co  m
using System;

public class Main{
        /// <summary>
        /// Recursive directory deletion that handles directories being opened in Windows Explorer.
        /// Source: http://stackoverflow.com/a/1703799
        /// </summary>
        private static void DeleteRecursive(string path)
        {
            foreach (var directory in Directory.GetDirectories(path))
            {
                DeleteRecursive(directory);
            }

            try
            {
                Directory.Delete(path, true);
            }
            catch (IOException)
            {
                Directory.Delete(path, true);
            }
            catch (UnauthorizedAccessException)
            {
                Directory.Delete(path, true);
            }
        }
}

Related Tutorials