Recursive delete of files and directories in C#

前端 未结 5 1742
暗喜
暗喜 2020-12-18 20:55

How to delete a given directory recursively in C# ? A directory containing files.

Should the System.IO.Directory.Delete with the second parameter true

5条回答
  •  没有蜡笔的小新
    2020-12-18 21:19

    Recursive works for both files and folders (oddly, I thought it didn't work for files; my bad...):

    // create some nested folders...
    Directory.CreateDirectory(@"c:\foo");
    Directory.CreateDirectory(@"c:\foo\bar");
    // ...with files...
    File.WriteAllText(@"c:\foo\blap.txt", "blup");
    File.WriteAllText(@"c:\foo\bar\blip.txt", "blop");
    // ...and delete them
    Directory.Delete(@"c:\foo", true); // fine
    

提交回复
热议问题