Private Function DelStoreFile(ByVal storeFilePath As String) As String
Try
Dim fileList As String() = Directory.GetFileSystemEntries(storeFilePath)
For Each filePath In fileList
If Directory.Exists(filePath) Then
Dim csvPath As String = DelStoreFile(filePath)
If csvPath IsNot Nothing Then
Return csvPath
Else
Directory.Delete(filePath)
End If
Else
Dim fileInfo As New FileInfo(filePath)
If fileInfo.Exists Then
If (fileInfo.Attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
fileInfo.Attributes = System.IO.FileAttributes.Normal
End If
fileInfo.Delete()
End If
End If
Next
Return Nothing
Catch ex As Exception
Return Nothing
End Try
End Function
C#
using System.IO;
using System.Linq;
namespace FileFolderDeleter
{
static class Program
{
static void Main(string[] args)
{
if (args.Count() == 1)
{
DeleteFilesAndFolders(args[0]);
}
}
/// <summary>
/// Recursively delete all the files and folders under the specific path.
/// </summary>
/// <param name="path">The specific path</param>
private static void DeleteFilesAndFolders(string path)
{
// Delete files.
string[] files = Directory.GetFiles(path);
foreach (var file in files)
{
File.Delete(file);
}
// Delete folders.
string[] folders = Directory.GetDirectories(path);
foreach (var folder in folders)
{
DeleteFilesAndFolders(folder);
Directory.Delete(folder);
}
}
}
}