Detect file format, by content, not extension

Peter Volz 1,295 Reputation points
2023-05-28T02:55:08.5366667+00:00

Hello expert devs

I need to detect the file format of JUST one file type:

oleData.mso

https://en.wikipedia.org/wiki/Compound_File_Binary_Format

Using FindMimeFromData will return Octet-Stream, no help

How to detect it?

There's OpenMcdf which I use to load the oleData.mso to it, if loaded with no exception, I consider the file to be in CFBF/ Mso format.

But this will need the whole dll to import to my VB project, any VB.net way to detect this special file type?

Please help if you can, appreciated :)

Developer technologies VB
Developer technologies C#
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2023-05-28T09:03:30.5133333+00:00

    Try something like this:

    Dim file_is_CFBF As Boolean
    
    Using fs = File.OpenRead("path to my file...")
        Dim bytes(8 - 1) As Byte
        Dim expected_bytes As Byte() = {&HD0, &HCF, &H11, &HE0, &HA1, &HB1, &H1A, &HE1}
        fs.Read(bytes, 0, 8)
        file_is_CFBF = bytes.SequenceEqual(expected_bytes)
    End Using
    
    MsgBox(file_is_CFBF)
    
    2 people 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.