LANCommander/LANCommander.Server/Extensions/FileExtensions.cs

32 lines
868 B
C#
Raw Permalink Normal View History

2024-08-04 18:44:33 -05:00
namespace LANCommander.Server.Extensions
{
public static class FileExtensions
{
public static bool IsBinaryFile(this FileInfo fileInfo, int checkLength = 8000, int requiredConsecutiveNul = 1)
{
const char nulChar = '\0';
int count = 0;
using (var reader = new StreamReader(fileInfo.FullName))
{
for (int i = 0; i < checkLength && !reader.EndOfStream; i++) {
if ((char)reader.Read() == nulChar)
{
count++;
if (count >= requiredConsecutiveNul)
return true;
}
else
{
count = 0;
}
}
}
return false;
}
}
}