C# - 检查文件是基于文本
问题描述:
如何测试是否是我在C#中使用的FileStream是一个文本类型文件打开一个文件?我想我的程序打开基于文本的任何文件,如.txt或.html等。
How can I test whether a file that I'm opening in C# using FileStream is a "text type" file? I would like my program to open any file that is text based, for example, .txt, .html, etc.
但不能打开这样的事情.doc或.pdf或.exe文件等。
But not open such things as .doc or .pdf or .exe, etc.
答
我想你可以只通过第一1000(任意数字)字符检查,看看是否有不可打印的字符,或者如果他们在一定范围内所有的ASCII 。如果是后者,假定它是文本?
I guess you could just check through the first 1000 (arbitrary number) characters and see if there are unprintable characters, or if they are all ascii in a certain range. If the latter, assume that it is text?
不管你做什么将是一个猜测。
Whatever you do is going to be a guess.