Creating and Deleting Files in a Console Application Using C#

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.

  • Open Visual Studio.
  • Select "Create a new project" or go to "File" > "New" > "Project."
  • Choose "Console App (.NET)" as the project template.
    Console Application to Create and Delete Files from folder using C#
  • Enter a name for your project (e.g., "FileDeletionApp") and Choose a location to save the project.
    Console Application to Create and Delete Files from folder using C#
  • Select Framework and Click on Next.
    Console Application to Create and Delete Files from folder using C#
  • Click "Create" to create the project.

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,

  • Create a folder on your computer and add some sample files to it. Note the folder path.
    Eg: "D:\TestDelete";
  • Update the folderPath variable in the code with the path of the folder you created.
    Eg: "D:\TestDelete";
  • Build and run the application. You will see the deletion process starting, followed by messages indicating each file that is deleted.
  • Once the deletion process completes, a message will be displayed. Press Enter to exit the application and review the results.

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,

  • Create a folder on your computer and Note the folder path.
    Eg: "D:\TestCreate";
  • Update the folderPath variable in the code with the path of the folder you created.
    Eg:  "D:\TestCreate";
  • Build and run the application. You will see the Creation process starting, followed by messages indicating each file is Created.
  • Every 15 Second new file will be created inside the folder we have specified.

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!".