using System;
using System.IO;
namespace FileManipulation
{
internal class FileDeleter
{
private static readonly string startSource = "D:\\Original";
/// <summary>
/// Below Function deletes the files with the name given
/// </summary>
internal void Delete()
{
// ask user for filename
Console.WriteLine("Enter the filename with extension. Which have to be deleted\n");
string fileName = Console.ReadLine();
// get all the files in a directory
string[] files = Directory.GetFiles(startSource, "*.*", SearchOption.AllDirectories);
int filesDeleted = 0;
// check if any file name matches the filename we received
foreach (string file in files)
{
if(new FileInfo(file).Name == fileName)
{
File.Delete(file);
if (!File.Exists(file))
{
filesDeleted++;
}
else
{
Console.WriteLine("Unable to delete file " + fileName);
}
}
}
Console.WriteLine("Successfully Deleted " + filesDeleted + " Files With Name " + fileName);
}
}
}
Delete Specific File From Directory Recursively Dot Net (.Net)
