The provided code demonstrates a simple console application that performs a file deletion and creation process at regular intervals using a timer.

Step 1. Create a new C# console application project. In Visual Studio, you can follow these steps.

Use the below code in Program.cs and replace all to delete the file from the folder.

Delete files from the folder

using System;
using System.IO;

public class Program {

  static void Main() {
    Console.WriteLine("Deletion process started");
    DeleteFiles();
    System.Timers.Timer timer = new System.Timers.Timer(60000);
    timer.Elapsed += TimerElapsed;
    timer.Start();

    Console.Read();

    timer.Stop();
    timer.Dispose();
  }

  static void DeleteFiles() {
    try {
      string folderPath = @ "D:\TestDelete";
      // Get all files in the folder
      string[] files = Directory.GetFiles(folderPath);

      foreach(string file in files) {
        // Delete each file
        File.Delete(file);
        Console.WriteLine($"Deleted file: {file}");
      }
    } catch (Exception ex) {
      Console.WriteLine($"An error occurred: {ex.Message}");
    }
  }

  static void TimerElapsed(object ? sender, System.Timers.ElapsedEventArgs e) {
    DeleteFiles();
  }
}

To test the code, follow these steps,

Use the below \code in Program.cs and Replace all to create the file in the folder.

Create dummy text files in a folder


using System;
using System.IO;
using System.Threading;

class Program
{
    static void Main()
    {
        Console.WriteLine("Creation process started");
        CreateFile();
        System.Timers.Timer timer = new System.Timers.Timer(15000);
        timer.Elapsed += TimerElapsed;
        timer.Start();

        Console.Read();

        timer.Stop();
        timer.Dispose();
    }

    static void CreateFile()
    {
        try
        {
            string folderPath = @"D:\TestCreate";
            string fileName = $"File_{DateTime.Now:yyyyMMdd_HHmmss}.txt";
            string filePath = Path.Combine(folderPath, fileName);

            // Create the file
            File.WriteAllText(filePath, "Sample file content");

            Console.WriteLine($"Created file: {filePath}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }

    static void TimerElapsed(object? state,System.Timers.ElapsedEventArgs e)
    {
        CreateFile();
    }
}

To test the code, follow these steps,

I hope this blog post has provided you with an understanding of ConsoleApplication to delete a file from a specific folder and create a file into a specific folder.

Thank you for reading, and happy coding!".