Delete All Files (*.*) [C#]
These examples show how to delete all files (*.*) from a folder in C#.
First, you need to get the list of file names from the specified directory (using static method Directory.GetFiles. Then delete all files from the list.
Delete all files
[C#]using System.IO; string[] filePaths = Directory.GetFiles(@"c:\MyDir\"); foreach (string filePath in filePaths) File.Delete(filePath);
Delete all files (one-row example)
To delete all files using one code line, you can use Array.ForEach with combination of anonymous method.
[C#]Array.ForEach(Directory.GetFiles(@"c:\MyDir\"), delegate(string path) { File.Delete(path); });
Files and Folders Examples
- [C#] FileStream Open File – how to open file using file stream
- [C#] FileStream Read File – how to safely read file stream
- [C#] Read Text File – how to read lines from text file
- [C#] Load Text File to String – how to load text from file to string
- [C#] Get Files from Directory – how to get files from directory
- [C#] Delete All Files – how to delete files from the specified directory
- [C#] Get Application Directory – how to get application or assembly folder
- [C#] File Attributes – how to get or set file attributes
- [C#] Get File Time – how to get last modification time of a file
- [C#] Open File With Associated Application – how to launch the default application
See also
- Directory.GetFiles – MSDN – returns the names of files in a specified directory
- Array.ForEach – MSDN – calls the specified method on each element of the array
Tips
- [C#] Foreach Examples – how foreach and IEnumerable works debuggable online