Funny Booboo Funny Booboo -4 years ago 69 Question

VB.NET System Deleting File

Hi I have made a code that deletes a .bmp file.


For Each foundFile As String In My.Computer.FileSystem.GetFiles(

Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.bmp")


However, I want to alter the code so that once there are more than 5 bmp files, the system deletes them.

I hope someone can help me thanks!

Answer Source

Get a list of files and check if the list contains more that five entries.

    Dim pth As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
        Dim fls() As String = IO.Directory.GetFiles(pth, "*.bmp", IO.SearchOption.TopDirectoryOnly)
        If fls.Length > 5 Then
            For Each f As String In fls
        End If
    Catch ex As Exception

    End Try
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download