File Type - Get Original Extension

How to find the file extension if the file has been renamed? Is there any tool for this?

Example: I have a file "1.doc"; I hope everyone knows that this is a Word document that I just renamed to "1.txt". But the file is originally a Word document; How can I get the original file extension?

+5
source share
3 answers

Of course you can :)

This is the C # code for you. I think you can intimidate your own tool;)

using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32;

    [DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
    private extern static System.UInt32 FindMimeFromData(
        System.UInt32 pBC,
        [MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
        [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
        System.UInt32 cbSize,
        [MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
        System.UInt32 dwMimeFlags,
        out System.UInt32 ppwzMimeOut,
        System.UInt32 dwReserverd
    );


    public static string getMimeFromFile(string filename)
    {
        if (!File.Exists(filename))
            throw new FileNotFoundException(filename + " not found");

        byte[] buffer = new byte[256];
        using (FileStream fs = new FileStream(filename, FileMode.Open))
        {
            if (fs.Length >= 256)
                fs.Read(buffer, 0, 256);
            else
                fs.Read(buffer, 0, (int)fs.Length);
        }
        try
        {
            System.UInt32 mimetype;
            FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
            System.IntPtr mimeTypePtr = new IntPtr(mimetype);
            string mime = Marshal.PtrToStringUni(mimeTypePtr);
            Marshal.FreeCoTaskMem(mimeTypePtr);
            return mime;
        }
        catch (Exception e)
        {
            return "unknown/unknown";
        }
    }

You get this view using this code. To find the extension from the mime type, just do a little google search.

+4
source

. * nix, file .

, ( ), 2 :

  • , , MD5 , ,
  • , , .

:

, .

, .

.

, 3.

, 99.99999999999% , - -.

+3

You can not. You will need to use a type tool fileto try to determine the file format.

+1
source

All Articles