0

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
user3500069
  • 3
  • 1
  • 3
  • Your first issue is that you have `On Error Resume Next` in there. Get rid of that and never use it again. Run the code again without that and see if an exception is thrown. That will give you a clue as to what the issue is. – jmcilhinney Apr 05 '14 at 00:34
  • Thanks alot sir it works fine now but how can i delet all the 3 files in one line code? like For Each file As String In Directory.GetFiles(path, "file1.sol"&"file12.sol"&"file3.sol") filelist.Add(file) – user3500069 Apr 05 '14 at 01:03

1 Answers1

0

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.

jlynch630
  • 687
  • 7
  • 15