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

查看:11
本文介绍了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天全站免登陆