List of images as HTML VB.Net Exercise - Visual Basic Programming Course


 Exercise

List of images as HTML

Objetive

Create a program that creates an HTML file that lists all the images (PNG and JPG) in the current folder.

For instance, if the current folder contains the following images:

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


Juan A. Ripoll - Systems Tutorials and Programming Courses © 2024 All rights reserved.  Legal Conditions.