C# - 检查文件是基于文本 [英] C# - Check if File is Text Based

查看:131
本文介绍了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.

这篇关于C# - 检查文件是基于文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆