List of images as HTML Learn programming with Visual Basic (VB.net) exercises

Lesson:

Additional Libraries


Exercise:

List of images as HTML 53


Objetive:

Create a program to create an HTML file containing the list of images (PNG and JPG) in the current directory.

For example, in the current directory there are images called.

1.png
2.jpg


Code:

Imports System
Imports System.IO
Imports System.Collections.Generic
Class ListImagesHTML
    Private Shared Sub Main()
        CreateHtml(GetImages())
    End Sub

    Private Shared Sub CreateHtml(ByVal listImages As List)
        Try
            Dim writer As StreamWriter = New StreamWriter(File.Create("images.html"))
            writer.WriteLine("")
            writer.WriteLine("")

            For Each image As String In listImages
                writer.WriteLine("" & image & "")
                writer.WriteLine(""");")
            Next

            writer.WriteLine("")
            writer.WriteLine("")
            writer.Close()
        Catch
            Console.WriteLine("Error writing html.")
        End Try
    End Sub

    Private Shared Function GetImages() As List
        Dim ListImages As List = New List()
        Dim files As String() = Directory.GetFiles(".")

        For Each file As String In files
            Dim extension As String = Path.GetExtension(file)

            Select Case extension
                Case ".png", ".jpg", ".jpge"
                    ListImages.Add(file.Substring(2))
            End Select
        Next

        Return ListImages
    End Function
End Class