C# Delete All Files In Directory

C# Delete All Files In Directory

c# delete files in directory

Sometimes we need to programmatically delete all files inside a directory.
There is a static method called GetFiles  in Directory class found at System.IO namespace
in .net base class library.

Code

using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string[] files = Directory.GetFiles(@"c:\MyDirectory");
foreach(string file in files)
{
FileInfo f = new FileInfo(file);
f.Delete();
}
} 
}
}


Description
Directory.GetFiles() returns array of fully qualified file names at specified directory.
Delete method of FileInfo instance referring to filepath can delete the file.

Delete all files and folders in a directory c#

Using recursion you can delete the files inside the subdirectories of specified path

Code

using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string directorypath = @"c:\MyDirectory";
string[] directories = Directory.GetDirectories(directorypath);
DeleteDirectories(directories);            
}
private static void DeleteDirectories(string[] directories)
{            
foreach(string directory in directories)
{
string[] files = Directory.GetFiles(directory);
DeleteFiles(files);
directories = Directory.GetDirectories(directory);             
DeleteDirectories(directories);
}
}
private static void DeleteFiles(string[] files)
{
foreach (string file in files)
{
FileInfo f = new FileInfo(file);
f.Delete();
}
}
}
}


Description:
DeleteDirectories function is called recursively. It finds all the files and delete them.
It navigates across the subdirectories and delete all found files.
Same way you can delete the directories which are empty.

Note: Use try catch blocks to catch the exception and log them to get appropriate action.

c# delete files in directory older than

You may want to delete files older than specific date or you may want to archive or move the files
somewhere.

Instance of FileInfo has method to get fiel creation date and time. You can compare the creation or modification date and time with current or specified date and time.
Collect the files which are older than specified date and time then delete those files.

This is very useful if you are creating log files daily at client side. Because logs file generated at client side are unattended and there exists possibility get run out of disk space due to heavy log files.

Removing of older log files from specified directory can be done at the startup of your program or
daily at specified time.

Code

using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string directorypath = @"d:\MyDirectory";
string[] files = Directory.GetFiles(directorypath);
foreach (string file in files)
{
FileInfo f = new FileInfo(file);
if (f.CreationTime < DateTime.Now)
{
f.Delete();
}
}
}       
}
}

Description
Instance of FileInfo has method named CreatinTime, it gives the creation time of the file.
You can compare it with current date time or you can compare it with your specified date and time.

 

Tags:

C#-move files from foldersC# Create DirectoryC# Get Current Directory of ExecutableAsp.net delete file file on Server C#.net and VB.net code(c#) write to text fileC# foreach loopVisual Studio Recover Deleted FileAsp.net Create Folder On ServerC# Get File Sizec# arraylistc# string

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.