Hi @StewartBW ,
You can attempt to read the first few bytes of the file and check for text patterns like <!DOCTYPE html>
(for HTML5) or <html>
(for older HTML versions).
Dim filePath As String = "yourfile.html"
Dim bufferSize As Integer = 4096 ' Read first 4KB of the file
Using fs As New System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
Dim buffer(bufferSize - 1) As Byte
fs.Read(buffer, 0, bufferSize)
Dim content As String = System.Text.Encoding.UTF8.GetString(buffer)
' Check for <!DOCTYPE html> or <html> tags in the initial bytes
If content.IndexOf("<!DOCTYPE html", StringComparison.OrdinalIgnoreCase) >= 0 OrElse
content.IndexOf("<html", StringComparison.OrdinalIgnoreCase) >= 0 Then
MessageBox.Show("This is an HTML file.")
Else
MessageBox.Show("This is not an HTML file.")
End If
End Using
Best Regards.
Jiachen Li
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.