In this post will guide you How to Delete a File in C#, with examples and Demo code, you can download for use.
The Delete method deletes the specified file permanently. The following code snippet deletes a file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Program { static void Main(string[] args) { string fileName = @"E:\file.txt"; FileInfo filetodelete = new FileInfo(fileName); try { filetodelete.Delete(); //delete the file Console.WriteLine("File deleted"); } catch (IOException ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); } } |