Delete directory by pattern - CSharp File IO

CSharp examples for File IO:Directory

Description

Delete directory by pattern

Demo Code


using System.IO;/*from   w ww . j  av  a2  s. co  m*/
using System.Collections.Generic;

public class Main{
        public static void Delete(this DirectoryInfo di, string searchParttern, bool recursive)
        {
            foreach (var fileInfo in di.GetFiles(searchParttern))
            {
                fileInfo.RemoveAttribute(FileAttributes.ReadOnly);
                fileInfo.RemoveAttribute(FileAttributes.System);
                fileInfo.Delete();
            }
            if (recursive)
                foreach (DirectoryInfo child in di.GetDirectories())
                {
                    child.Delete(searchParttern, recursive);
                }
        }
}

Related Tutorials