How to delete all files and folders in a directory in C#?
Hello everyone! I'm a beginner C# developer and I've encountered this problem. I need to write a function that clears a folder of all files and subfolders. I've tried several methods, but something always goes wrong.\n\nHere's my code:\n\n```csharp\nstring path = @"C:\\Temp\\MyFolder";\nif (Directory.Exists(path))\n{\n Directory.Delete(path, true);\n Directory.CreateDirectory(path);\n}\n```\n\nThe problem is that when trying to delete a folder with files that are being used by another process, an IOException is thrown. And if I try to delete files one by one using File.Delete, the folders remain and need to be deleted recursively, but I can't figure out the nesting.\n\nI also tried this option:\n\n```csharp\nforeach (string file in Directory.GetFiles(path))\n{\n File.Delete(f