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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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