Check if a file is an executable.


/ Published in: C#
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments


You need to login to view comments.