file.delete c# how to delete more files code example
Example: how to delete multiple file in c#
string sourceDir = @"c:\current";
string backupDir = @"c:\archives\2008";
try
{
string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
string[] txtList = Directory.GetFiles(sourceDir, "*.txt");
foreach (string f in picList)
{
string fName = f.Substring(sourceDir.Length + 1);
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
}
foreach (string f in txtList)
{
string fName = f.Substring(sourceDir.Length + 1);
try
{
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
}
catch (IOException copyError)
{
Console.WriteLine(copyError.Message);
}
}
foreach (string f in txtList)
{
File.Delete(f);
}
foreach (string f in picList)
{
File.Delete(f);
}
}
catch (DirectoryNotFoundException dirNotFound)
{
Console.WriteLine(dirNotFound.Message);
}