Get file name from byte array or Stream

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.


No this isn't possible (ok so it might be possible on the FileStream class, learn something new everyday!).

A byte array or stream represents the content of the file, not the Windows metadata about the file.

There are plenty of straightfoward ways to retain this information, but not knowing any more about your situation I can't offer a solution.


If the Stream is actually a FileStream, then this may be available by casting to FileStream and accessing the .Name property:

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. 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.

I would handle the filename separately.