Get file type in .NET

后端 未结 5 1350
野性不改
野性不改 2020-12-02 19:22

How can i get type of file using c#. for example if a file name id \"abc.png\" and type of file will \"PNG Image\" same as third column \"Type\" in window explorer.

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 20:03

    P/invoke to SHGetFileInfo, and check szDisplayName in the returned structure. The result will depend on how you have your file types defined (i.e. it won't be an absolute reference). But it should be fine in most cases. Click here for the c# signature of SHGetFileInfo and example code on pinvoke.net (awesome site that it is)

    For an absolute reference you will need something that checks a few bytes in the binary header and compares against a known list of these bytes - I think this is how unix-based systems do it by default.

提交回复
热议问题