Sitemap creator Learn programming Visual Basic (VB.net)

Lesson:

Additional Libraries


Exercise:

Sitemap creator 35


Objetive:

A "sitemap" is a file that webmasters can use in order to tell Google the webpages that their site consists on, and get a better positioning in the search engine.

Yo must create a program to show on screen the contents of a preliminary "sitemap", taken from the list of ".html" files in the current directory, with "weekly" frequence and the current date as "last modification" date.


Code:

Imports System
Imports System.Collections.Generic
Imports System.IO
Class SitemapCreator
    Private Shared Sub Main()
        Dim ListHtml As List = GetHtml()
        CreateSiteMap(ListHtml, "weekly", DateTime.Now)
    End Sub

    Private Shared Sub CreateSiteMap(ByVal listHtml As List, ByVal frecuency As String, ByVal lastUpdated As DateTime)
        Try
            Dim writer As StreamWriter = New StreamWriter(File.Create("sitemap.xml"))
            writer.WriteLine("")
            writer.WriteLine("")

            For Each html As String In listHtml
                writer.WriteLine("")
                writer.WriteLine("" & html & "")
                writer.WriteLine("" & lastUpdated.ToShortDateString() & "")
                writer.WriteLine("" & frecuency & "")
                writer.WriteLine("")
            Next

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

    Private Shared Function GetHtml() As List
        Dim ListHtml 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 ".html", ".htm"
                    ListHtml.Add(file.Substring(2))
            End Select
        Next

        Return ListHtml
    End Function
End Class