Using C#, how can I delete all files and folders from a directory, but still keep the root directory?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Call from main
Add this method
The only thing you should do is to set
optional recursive parameter
toTrue
.Directory.Delete("C:\MyDummyDirectory", True)
Thanks to .NET. :)
Yes, that's the correct way to do it. If you're looking to give yourself a "Clean" (or, as I'd prefer to call it, "Empty" function), you can create an extension method.
This will then allow you to do something like..
this will show how we delete the folder and check for it we use Text box
If your directory may have many files,
EnumerateFiles()
is more efficient thanGetFiles()
, because when you useEnumerateFiles()
you can start enumerating it before the whole collection is returned, as opposed toGetFiles()
where you need to load the entire collection in memory before begin to enumerate it. See this quote here:The same applies to
EnumerateDirectories()
andGetDirectories()
. So the code would be:For the purpose of this question, there is really no reason to use
GetFiles()
andGetDirectories()
.