简体   繁体   English

从字节数组或流中获取文件名

[英]Get file name from byte array or Stream

Is it possible to get filename from the byte array or stream?是否可以从字节数组或流中获取文件名? I wont to save the file.我不会保存文件。 I just want to retrieve its name.我只想找回它的名字。

If the Stream is actually a FileStream , then this may be available by casting to FileStream and accessing the .Name property:如果Stream实际上是一个FileStream ,那么这可能是可通过转换成FileStream和访问.Name属性:

Stream stream = ...
FileStream fs = stream as FileStream;
if(fs != null) Console.WriteLine(fs.Name);

However, in the general case : no, this is not available.但是,在一般情况下:不,这是不可用的。 A byte[] certainly has no concept of a filename, nor do most other types of streams. byte[]当然没有文件名的概念,大多数其他类型的流也没有。 Likewise, a FileStream base-stream that is being wrapped by other streams (compression, encryption, buffering, etc) will not expose such information, despite the underlying stream (several layers down) being a file.同样,由其他流(压缩、加密、缓冲等)包装的FileStream基本流不会公开此类信息,尽管底层流(向下几层)是一个文件。

I would handle the filename separately.我会单独处理文件名。

No this isn't possible (ok so it might be possible on the FileStream class, learn something new everyday!).不,这是不可能的(好吧,所以它可能在FileStream类上是可能的,每天学习新东西!)。

A byte array or stream represents the content of the file, not the Windows metadata about the file.字节数组或流表示文件的内容,而不是有关文件的 Windows元数据

There are plenty of straightfoward ways to retain this information, but not knowing any more about your situation I can't offer a solution.有很多直接的方法可以保留这些信息,但对您的情况一无所知,我无法提供解决方案。

All file information (such as name, extension etc.) is part of meta data for an actual file.所有文件信息(例如名称、扩展名等)都是实际文件的元数据的一部分。 The byte array will only hold the actual data.字节数组将只保存实际数据。 It may be possible if the byte array itself holds meta data (example an xml file)... however, you'd need to know the type and specifically where to look.如果字节数组本身包含元数据(例如 xml 文件),则可能是可能的……但是,您需要知道类型以及具体的查找位置。

You will not able to get filename from byte array.您将无法从字节数组中获取文件名。 Instead you need filestream to get the name of the file.相反,您需要 filestream 来获取文件的名称。 Byte array does not store name.字节数组不存储名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM