How to determine the type of file?

I need to know if there is an audio file in my file: mp3, wav, etc.
How to do it?

+5
source share
5 answers

Well, the most reliable way would be to write a parser for the types of files you want to detect, and then just try - if there are no errors, this is clearly the type you tried. However, this is an expensive approach, but it will also ensure that the file is uploaded successfully, since it also checks the rest of the file for semantic reliability.

"" - . , ID3, , MP3. RIFF¼↕☻ WAVEfmt, WAV . , - .

+5

, , 100% . Windows, 99,9% , Windows , .

, Windows, - , . , ID3 mp3 :

ID3v1 128 , TAG.

ID3v2 , .

, , , , , .

, - mp3 (), , , (, ).

+2

, :

using System.IO;

class Program
{
    static void Main()
    {
        string filepath = @"C:\Users\Sam\Documents\Test.txt";

        string extension = Path.GetExtension(filepath);
        if (extension  == ".mp3")
        {
            Console.WriteLine(extension);
        }
    }
}

- , , 100%, - . , , , .

You can check this post on an old post to help a bit. Here is a message about finding only media file types.

Ultimately, it depends on why you are trying to do this.

0
source

See this post. As a result, you transfer the first (up to) 256 bytes of data from the file to FindMimeFromData (part of Urlmon.dll).

0
source
Path.GetExtension(PathToFile)
-1
source

All Articles