确定给定 FileStream 的文件扩展名
有没有办法知道FileStream
的类型。我有一个接受 FileStream
对象的函数,我想根据该 FileStream
确定文件扩展名。
Is there any way to know the type of the FileStream
. I have a function that takes a FileStream
object and I want to determine the file extension based on that FileStream
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果流确实是一个
FileStream
,那么您应该能够执行以下操作如果它是一个普通的旧
Stream
,那么通常不可能获得扩展名,因为>Stream
可以为任何字节流创建。它不必有备份文件。更新
正如 Chris 在评论中指出的那样,还有另一个与以下相关的问题:这次讨论。它讨论了确定
byte[]
类型的启发式方法,然后可以将其映射到可能的原始签名。这绝不是万无一失的,但可能对您有帮助。
If the stream is really a
FileStream
then you should be able to do the followingIf it's a plain old
Stream
though then it's not generally possible to get the extension because aStream
can be created for any stream of bytes. It doesn't have to have a backing file.Update
As Chris pointed out in the comments there is another SO question which is relevant to this discussion. It's discussing heuristics for determining type of a
byte[]
which can then be mapped to a probable original signature.It's by no means foolproof but may be helpful to you.
是的,使用文件名以下将返回
.txt
(包括.
):Yes, using the file name the following will return
.txt
(including the.
):