Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I convert a Stream object to a FileInfo object?

Tags:

c#

asp.net-mvc

For the ExcelPackage constructor you need a FileInfo object. I rather use some kind of stream object(f.i. MemoryStream), because I don't need to save the file to the server itself, but expose it as a FileStream anyway to the user. I don't want to make files which I have to delete lateron from servers which are only there for generating purposes and never used again. Apart from that, otherwise I need also the necessary rights for the application/user on the directory/file on the server.

So my question is then: How can I convert a stream object to a FileInfo object.

like image 377
Michael Avatar asked Mar 17 '10 13:03

Michael


People also ask

What is used to convert stream of bytes to object?

Deserializing (converting byte array into Object)

Which of the following is are the properties of the FileInfo?

C# FileInfo PropertiesIt is used to get or set the creation time of the current file or directory. It is used to get an instance of the parent directory. It is used to get a string representing the directory's full path. It is used to get a value indicating whether a file exists.

What is System IO FileInfo?

FileInfo Class (System.IO)Provides properties and instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.


3 Answers

Yes you can

var excelPackage = new ExcelPackage();
excelPackage.Load(fileStream);
like image 170
Cătălin Rădoi Avatar answered Oct 22 '22 09:10

Cătălin Rădoi


You can't convert the Stream as such to a FileInfo; they represent entirely different things. A Stream contains data thay may or may not represent a file on disk. A FileInfo on the other hand contains metadata about a file, that may or may not exist.

What you can do is to write the contents of the Stream to a file on disk, create a FileInfo pointing at that file and pass that FileInfo to the constructor.

like image 24
Fredrik Mörk Avatar answered Oct 22 '22 08:10

Fredrik Mörk


A FileInfo class is a simple wrapper around a path to a file on disk.
It is not possible to have a FileInfo wrap a memory stream.

However, you can download the source code and add a constructor that takes a Stream. (The file path is only used in the WriteDebugFile method)

like image 28
SLaks Avatar answered Oct 22 '22 08:10

SLaks