How to delete a file in C#
By Tan Lee Published on Jun 11, 2024 466
In C#, you can delete a file using the File.Delete() method from the System.IO namespace.
Here's a code snippet deletes a file in C#.
using System; using System.IO; class Program { static void Main() { // Specify the path of the file you want to delete string filePath = @"C:\path\yourfile.txt"; try { // Check if the file exists before attempting to delete it if (File.Exists(filePath)) { // c# delete the file File.Delete(filePath); } else { Console.WriteLine("File does not exist."); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
Make sure to replace "C:\path\yourfile.txt"
with the actual path of the file you want to delete.
Categories
Popular Posts
11 Things You Didn't Know About Cloudflare
Dec 19, 2024
Gentella Admin Template
Nov 14, 2024
Focus Admin Dashboard Template
Nov 18, 2024