Get file name from byte array or Stream

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.

Leave a Comment

tech