Posted By

jprochazka on 05/18/13


Tagged

validation


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

danfinrud55
emregulcan


Check if a file is an executable.


 / Published in: C#
 

A function to check the first two bytes to see if they match "MZ" marking it as an executable.

  1. bool CheckIsExecutable(string filePath)
  2. {
  3. var firstBytes = new byte[2];
  4. using(var fileStream = File.Open(filePath, FileMode.Open))
  5. {
  6. fileStream.Read(firstBytes, 0, 2);
  7. }
  8. return Encoding.UTF8.GetString(firstBytes) == "MZ";
  9. }

Report this snippet  

You need to login to post a comment.