How to find if a file is an exe?

后端 未结 6 1756
粉色の甜心
粉色の甜心 2020-12-05 14:51

How can I be sure that a file passed to my program is a valid exe file ?

actually my program takes a file as input and runs it, but user can input any file so I have

6条回答
  •  有刺的猬
    2020-12-05 15:20

    If you want to just check the file extension:

    if (String.Equals(Path.GetExtension(filePath), ".exe", StringComparison.InvariantCultureIgnoreCase))
    {
        ...
    }
    

    If you need to verify that the file is really executable you can analyze its header. There is information about .exe file header: link.

    You also can try to run the file as 0xA3 suggested

提交回复
热议问题