delete a csv file in csharp

To delete a CSV file in C#, you can use the System.IO namespace and its File class. Here's how you can delete a CSV file in C#:

main.cs
using System.IO;

string filePath = @"C:\example\file.csv";

if (File.Exists(filePath))
{
    File.Delete(filePath);
}
119 chars
9 lines

In the above code:

  • We first import the System.IO namespace to access the File class.
  • We define the file path of the CSV file using a string variable named filePath.
  • We use the File.Exists method of the File class to check whether the file exists at the specified filePath.
  • If the file exists, we use the File.Delete method of the File class to delete the file at the specified filePath.

Note that deleting a file is a permanent action, so be sure that you really want to delete the file before executing this code.

related categories

gistlibby LogSnag