Funny Booboo Funny Booboo -4 years ago 69
Vb.net Question

VB.NET System Deleting File

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

Try

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

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

System.IO.File.Delete(foundFile)


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)
    Try
        Dim fls() As String = IO.Directory.GetFiles(pth, "*.bmp", IO.SearchOption.TopDirectoryOnly)
        If fls.Length > 5 Then
            For Each f As String In fls
                IO.File.Delete(f)
            Next
        End If
    Catch ex As Exception

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