Tuesday 7 April 2015

C# - Delete all the files starting with 'Test'.

1. All the files are present in the folder at path "C:\files\"

2.Fetch all the file names starting with "Test".
  string[] filePaths = Directory.GetFiles(@"C:\files\","Test*.txt", SearchOption.AllDirectories);

Sample Code:
using System;
using System.IO;
namespace ConsoleProj
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string[] filePaths = Directory.GetFiles(@"C:\files\","Test*.txt", SearchOption.AllDirectories);
         
   foreach (string filePath in filePaths)
            {
                Console.WriteLine(filePath);
                File.Delete(filePath);
            }
            Console.ReadLine ();
        }
    }
}

3. Execute the below code.

4. Files with the file names starting with "Test" are deleted.


No comments:

Post a Comment