如何确定文件是c#中的二进制文件还是文本文件

How can I determine if a file is binary or text in c#?(如何确定文件是c#中的二进制文件还是文本文件?)
本文介绍了如何确定文件是c#中的二进制文件还是文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定 80% 的文件是二进制文件还是文本文件,有没有什么方法可以在 c# 中快速和肮脏/丑陋?

I need to determine in 80% if a file is binary or text, is there any way to do it even quick and dirty/ugly in c#?

推荐答案

我可能会寻找大量的控制字符,这些字符通常出现在二进制文件中,但很少出现在文本文件中.二进制文件倾向于使用足够的 0,以至于仅测试许多 0 字节可能足以捕获大多数文件.如果您关心本地化,您还需要测试多字节模式.

I would probably look for an abundance of control characters which would typically be present in a binary file but rarely in an text file. Binary files tend to use 0 enough that just testing for many 0 bytes would probably be sufficient to catch most files. If you care about localization you'd need to test multi-byte patterns as well.

如前所述,您总是很不幸,得到一个看起来像文本的二进制文件,反之亦然.

As stated though, you can always be unlucky and get a binary file that looks like text or vice versa.

这篇关于如何确定文件是c#中的二进制文件还是文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)