Delete Directory Files - CSharp File IO

CSharp examples for File IO:Directory

Description

Delete Directory Files

Demo Code


using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;
using System.IO.Compression;
using System.IO;/*from w  ww .j  a  va2  s .  c om*/
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using System;

public class Main{
        public static void DeleteDirectoryFiles(string TargetDir, bool delSubDir)
        {
            foreach (string fileName in Directory.GetFiles(TargetDir))
            {
                File.SetAttributes(fileName, FileAttributes.Normal);
                File.Delete(fileName);
            }
            if (delSubDir)
            {
                DirectoryInfo dir = new DirectoryInfo(TargetDir);
                foreach (DirectoryInfo subDi in dir.GetDirectories())
                {
                    DeleteDirectoryFiles(subDi.FullName, true);
                    subDi.Delete();
                }
            }
        }
}

Related Tutorials