从没有扩展名的文件中获取扩展名

joe*_*oei -3 .net c# winforms

是否有任何方法可以获得(实际猜测!)扩展文件?在我的程序中,我得到一个文件,应用程序分析它以理解它是ZIP还是MOV.

我发现了这个,但它不支持MOV和ZIP.

更新:

通过创建包含文件签名的前8位的文本文件.以下代码,我可以确定每个没有扩展名的文件. 这个页面可能是一个很好的参考.

        string rootPath = $"{name}";
        using (FileStream fsSource = new FileStream(rootPath, FileMode.Open, FileAccess.Read))
        {
            byte[] fileBytes = new byte[8]; // the number of bytes you want to read
            fsSource.Read(fileBytes, 0, 8);

            /*
             zip = 50-4B-03-04-0A-00-00-00
             mov = 00-00-00-20-66-74-79-70
             html = 3C-21-64-6F-63-74-79-70
             rar 1 = 52-61-72-21-1A-07
             rar 5 = 52-61-72-21-1A-07 

            */
            string filestring = BitConverter.ToString(fileBytes);
            // string filestring = Encoding.UTF8.GetString(fileBytes); 
            File.WriteAllText($"{DownloadPath}\\filestring.txt", filestring);
        }
Run Code Online (Sandbox Code Playgroud)

Ash*_*ani 5

只需读取文件的标题部分(文件开头的几个字节),您就可以检测其格式.

例如,此页面包含有关mov文件的信息.

您可以像这样的代码读取文件头(这里我假设读取4个字节就足够了,但是,如果您需要更多/更少的字节来确定格式,您可以根据需要更改它):

 using (FileStream fsSource = new FileStream(pathSource, FileMode.Open, FileAccess.Read))
 {
     byte[] bytes = new byte[4]; // the number of bytes you want to read
     fsSource.Read(bytes, 0, 4);
 }
Run Code Online (Sandbox Code Playgroud)