您的位置:首页 > 其它

判断文件是否文本文件还是二进制文件的两个有效方法

2012-03-03 00:31 387 查看
/// <summary>
/// 判断文件是否是二进制文件
/// </summary>
/// http://www.xuehai.net/
/// <param name="filePath">文件路径</param>
/// <returns>返回True为二进制文件,否则是文本文件</returns>
public bool IsBinaryFile(string filePath)
{
FileStream fs = File.OpenRead(filePath);
int len = (int)fs.Length;
int count = 0;
byte[] content = new byte[len];
int size = fs.Read(content, 0, len);

for (int i = 0; i < size; i++)
{
if (content[i] == 0)
{
count++;
if (count == 4)
{
return true;
}
}
else
{
count = 0;
}
}
return false;
}

/// <summary>
/// 判断文件是否是文本文件
/// </summary>
/// http://www.xuehai.net/
/// <param name="filePath">文件路径</param>
/// <returns>返回True为文本文件,否则是二进制文件</returns>
public bool IsTextFile(string filePath)
{
FileStream file = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] byteData = new byte[1];
while (file.Read(byteData, 0, byteData.Length) > 0)
{
if (byteData[0] == 0)
return false;
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: