Reputation: 3
I'm trying to delete 3 files (file1.sol file2.sol file3.sol) from the Application Data folder. My code words just fine with one file, but how can I make it delete the three files?
Here is my code:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim fileList As New List(Of String)
GetAllAccessibleFiles(path, fileList)
Application.DoEvents()
Dim files As String() = fileList.ToArray
For Each s As String In fileList
My.Computer.FileSystem.DeleteFile(s)
Next
End Sub
Sub GetAllAccessibleFiles(ByVal path As String, ByVal filelist As List(Of String))
For Each file As String In Directory.GetFiles(path, "file1.sol")
filelist.Add(file)
Next
For Each file As String In Directory.GetFiles(path, "file2.sol")
filelist.Add(file)
Next
For Each file As String In Directory.GetFiles(path, "file3.sol")
filelist.Add(file)
Next
For Each dir As String In Directory.GetDirectories(path)
Try
GetAllAccessibleFiles(dir, filelist)
Catch ex As Exception
End Try
Next
End Sub
Upvotes: 0
Views: 325
Reputation: 697
The first line in GetAllAccesibleFiles
, is searching for files with the name: "file1.sol", that means it will only retrieve that file. Try "file*.sol" meaning it starts with "file" and ends with ".sol". That should work. If you only want to delete file1 ,2 ,and 3, not for example, file4, then you can run a for loop 3 times, check if "file1" exists, delete it, check if "file2" exists, and so on.
Upvotes: 0