gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
Write a C# program to delete a folder with all of its content (files & sub-folders)
- User
Write a C# program to delete a folder with all of its content (files & sub-folders)
- Assistant
Sure! Here's a C# program to delete a folder and all its contents:
using System; using System.IO; class Program { static void Main(string[] args) { string folderPath = @"C:\Path\To\Folder"; // Replace with your folder path try { DeleteFolder(folderPath); Console.WriteLine("Folder deleted successfully."); } catch (Exception ex) { Console.WriteLine("Error deleting folder: " + ex.Message); } Console.ReadKey(); } static void DeleteFolder(string path) { if (!Directory.Exists(path)) { throw new DirectoryNotFoundException($"Directory '{path}' does not exist."); } foreach (string file in Directory.GetFiles(path)) { File.Delete(file); } foreach (string subDirectory in Directory.GetDirectories(path)) { DeleteFolder(subDirectory); } Directory.Delete(path); } }You need to replace
"C:\Path\To\Folder"in thefolderPathvariable with the actual path of the folder you want to delete.