Get html file title tag

StewartBW 1,145 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.

C#
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.
10,925 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,727 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,321 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 Answers by the question author, which helps users to know the answer solved the author's problem.