C# – 检查文件是否基于文本

如何使用FileStream测试我在C#中打开的文件是否为“文本类型”文件? 我希望我的程序打开任何基于文本的文件,例如.txt,.html等。

但不要打开.doc或.pdf或.exe等等。

我想你可以检查前1000个(任意数字)字符,看看是否有不可打印的字符,或者它们是否都在某个范围内的ascii。 如果是后者,假设它是文本?

无论你做什么都是猜测。

一般来说:没有办法说出来。

如果使用8位编码打开,以UTF-16格式存储的文本文件可能看起来像二进制文件。 同样有人可以将文本文件保存为.doc (它是一个文档)。

虽然您可以打开文件并查看一些内容,但所有这些启发式方法有时会失败(例如,记事本尝试这样做,通过仔细选择几个字符,记事本会猜错并显示完全不同的内容)。

如果你有一个特定的场景,而不是能够打开和处理任何东西,你应该能够做得更好。

要获取文件的实际类型 ,必须检查其标题,即使修改了扩展名也不会更改。 您可以在此处获取标题列表,并在代码中使用以下内容:

 using(var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { using(var reader = new BinaryReader(stream)) { // read the first X bytes of the file // In this example I want to check if the file is a BMP // whose header is 424D in hex(2 bytes 6677) string code = reader.ReadByte().ToString() + reader.ReadByte().ToString(); if (code.Equals("6677")) { //it's a BMP file } } } 

我有一个适合我的下面的解决方案。这是检查所有类型的二进制文件的通用解决方案。

  ///  /// This method checks whether selected file is Binary file or not. ///  public bool CheckForBinary() { Stream objStream = new FileStream("your file path", FileMode.Open, FileAccess.Read); bool bFlag = true; // Iterate through stream & check ASCII value of each byte. for (int nPosition = 0; nPosition < objStream.Length; nPosition++) { int a = objStream.ReadByte(); if (!(a >= 0 && a <= 127)) { break; // Binary File } else if (objStream.Position == (objStream.Length)) { bFlag = false; // Text File } } objStream.Dispose(); return bFlag; } 
 public bool IsTextFile(string FilePath) using (StreamReader reader = new StreamReader(FilePath)) { int Character; while ((Character = reader.Read()) != -1) { if ((Character > 0 && Character < 8) || (Character > 13 && Character < 26)) { return false; } } } return true; }