Share via

Get html file title tag

StewartBW 1,905 Reputation points
2024-04-05T03:00:05.12+00:00

Hello

Need to get the title of am html document without any third party components, regex is not a safe method, the most assured method I found was using a WebBrowser control.

Using MyWebBrowser As New WebBrowser
    MyWebBrowser.Navigate("about:blank")
    If MyWebBrowser.Document IsNot Nothing Then
        MyWebBrowser.Document.Write(FileReadAllText(InputFile))
        Return MyWebBrowser.Document.Title
    End If
End Using

No idea if using it correctly, so my questions are:

If MyWebBrowser.Document IsNot Nothing Then... is this needed and used in correct line?

Am I loading html and getting title correctly?

Thanks for adivse.

Developer technologies | VB
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments
{count} votes

Answer accepted by question author
  1. Peter Fleischer (former MVP) 19,351 Reputation points
    2024-04-05T06:09:41.8266667+00:00

    Hi,
    using WebBrowser you must wait for document ready. Try following console demo:

    Imports System.IO
    Imports System.Windows.Forms
    
    Module Module87
    
      Sub Main()
        Try
          Call (New Demo).Execute()
        Catch ex As Exception
          Console.WriteLine(ex.ToString)
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
    
      Friend Class Demo
        Friend Sub Execute()
          Dim ApplicationDirectory = Path.GetDirectoryName(Application.ExecutablePath)
          Dim myFile = Path.Combine(ApplicationDirectory, "Module87.html")
          Dim ret = GetTitleForUri(New Uri("file:///" & myFile))
          Console.WriteLine(ret)
        End Sub
      End Class
    
      Function GetTitleForUri(input As Uri) As String
        Using wb As New WebBrowser
          wb.Navigate(input)
          While (wb.ReadyState <> WebBrowserReadyState.Complete)
            Application.DoEvents()
          End While
          Return wb.Document?.Title
        End Using
      End Function
    
    End Module
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.