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.
No. The byte[] has the contents of the file, no more, no less.
You will not able to get filename from byte array. Instead you need filestream to get the name of the file. Byte array does not store name.
Streams are commonly written to byte arrays. The byte array is the preferred way to reference binary data in . NET. It can be used to data like the contents of a file or the pixels that make up the bitmap for an image.
From expirience I can say, that it does not copy the array. Be aware though, that you are unable to resize the memory stream, when using an array in the constructor.
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.
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With