Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 938
Gus Avatar asked Jan 05 '12 11:01

Gus


People also ask

Can we get file name from byte array Java?

No. The byte[] has the contents of the file, no more, no less.

Can we get file name from byte array C#?

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.

Is a byte array a stream?

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.

Does Memorystream copy byte array?

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.


2 Answers

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.

like image 146
Marc Gravell Avatar answered Oct 05 '22 00:10

Marc Gravell


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.

like image 31
Adam Houldsworth Avatar answered Oct 05 '22 02:10

Adam Houldsworth