Problem locating unix executables #MG5

Here is the function fixed for those that are wondering:

public bool IsFileExecutable(string filePath)
{

    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
        if (new string[] { ".dll" }.Contains(new FileInfo(filePath).Extension))
        {
            return false;
        }

        byte[] firstBytes = new byte[2];

        try
        {
            using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                if (fs.Length >= 2)
                {
                    fs.Read(firstBytes, 0, 2);
                }
            }
        }
        catch
        {
            return false;
        }

        return Encoding.UTF8.GetString(firstBytes) == "MZ"; 
    }
    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { 
        UnixFileMode mode = File.GetUnixFileMode(filePath);
        UnixFileMode perms = UnixFileMode.UserExecute | UnixFileMode.UserRead;
        return (mode & perms) == perms;
    }

    return false;
}
1 Like