确定给定 FileStream 的文件扩展名

发布于 2024-12-27 17:08:04 字数 116 浏览 0 评论 0原文

有没有办法知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酷炫老祖宗 2025-01-03 17:08:04
 string extension = Path.GetExtension(myFileStream.Name);
 string extension = Path.GetExtension(myFileStream.Name);
少女情怀诗 2025-01-03 17:08:04

如果流确实是一个FileStream,那么您应该能够执行以下操作

var ext = Path.GetExtension(fileStream.Name);

如果它是一个普通的旧Stream,那么通常不可能获得扩展名,因为>Stream 可以为任何字节流创建。它不必有备份文件。

更新

正如 Chris 在评论中指出的那样,还有另一个与以下相关的问题:这次讨论。它讨论了确定 byte[] 类型的启发式方法,然后可以将其映射到可能的原始签名。

这绝不是万无一失的,但可能对您有帮助。

If the stream is really a FileStream then you should be able to do the following

var ext = Path.GetExtension(fileStream.Name);

If it's a plain old Stream though then it's not generally possible to get the extension because a Stream 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.

空心空情空意 2025-01-03 17:08:04

是的,使用文件名以下将返回 .txt (包括 .):

var path = myFileStream.Name;
return Path.GetExtension(path);

Yes, using the file name the following will return .txt (including the .):

var path = myFileStream.Name;
return Path.GetExtension(path);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文