12

Given a stream object which contains an xlsx file, I want to save it as a temporary file and delete it when not using the file anymore.

I thought of creating a class that implementing IDisposable and using it with the using code block in order to delete the temp file at the end.

Any idea of how to save the stream to a temp file and delete it on the end of use?

Thanks

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108

5 Answers5

30

You could use the TempFileCollection class:

using (var tempFiles = new TempFileCollection())
{
    string file = tempFiles.AddExtension("xlsx");
    // do something with the file here 
}

What's nice about this is that even if an exception is thrown the temporary file is guaranteed to be removed thanks to the using block. By default this will generate the file into the temporary folder configured on the system but you could also specify a custom folder when invoking the TempFileCollection constructor.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
13

You can get a temporary file name with Path.GetTempFileName(), create a FileStream to write to it and use Stream.CopyTo to copy all data from your input stream into the text file:

var stream = /* your stream */
var fileName = Path.GetTempFileName();

try
{
    using (FileStream fs = File.OpenWrite(fileName))
    {
        stream.CopyTo(fs);
    }

    // Do whatever you want with the file here
}
finally
{
    File.Delete(fileName);
}
Jon
  • 428,835
  • 81
  • 738
  • 806
8

Another approach here would be:

string fileName = "file.xslx";
int bufferSize = 4096;

var fileStream = System.IO.File.Create(fileName, bufferSize, System.IO.FileOptions.DeleteOnClose)

// now use that fileStream to save the xslx stream

This way the file will get removed after closing.

Edit:

If you don't need the stream to live too long (eg: only a single write operation or a single loop to write...), you can, as suggested, wrap this stream into a using block. With that you won't have to dispose it manually.

Code would be like:

string fileName = "file.xslx";
int bufferSize = 4096;

using(var fileStream = System.IO.File.Create(fileName, bufferSize, System.IO.FileOptions.DeleteOnClose))
{
    // now use that fileStream to save the xslx stream
}
RedRose
  • 555
  • 7
  • 27
Anderson Matos
  • 3,132
  • 1
  • 23
  • 33
1
// Get a random temporary file name w/ path:
string tempFile = Path.GetTempFileName();

// Open a FileStream to write to the file:
using (Stream fileStream = File.OpenWrite(tempFile)) { ... }

// Delete the file when you're done:
File.Delete(tempFile);

EDIT:

Sorry, maybe it's just me, but I could have sworn that when you initially posted the question you didn't have all that detail about a class implementing IDisposable, etc... anyways, I'm not really sure what you're asking in your (edited?) question. But this question: Any idea of how to save the stream to temp file and delete it on the end of use? is pretty straight-forward. Any number of google results will come back for ".NET C# Stream to File" or such.

Steve
  • 31,144
  • 19
  • 99
  • 122
0

I just suggest for creating file use Path.GetTempFileName(). but others depends on your usage senario, for example if you want to create it in your temp creator class and use it just there, it's good to use using keyword.

Saeed Amiri
  • 22,252
  • 5
  • 45
  • 83