Temporary File Stream
// December 8th, 2009 // Useful Code
This short class is perfect for creating a temporary stream to disk incase you are working with a very large amount of data. I recently had to use this class for certain circumstances when chaining large dataset streams in a Soap compression library. Because of IIS’s memory limitation the only way to preform the compression and un-compression without running out of RAM was to stream to disk.
class TempFileStream : FileStream
{
/// <summary>
/// STORE THE NAME OF THE FILE WHICH WE OPENED SO WE CAN DELETE IT WHEN THE STREAM IS CLOSED
/// </summary>
private string mFileName = null;
public TempFileStream(string path, FileMode mode, FileAccess access, FileShare share)
: base(path, mode, access, share)
{
//REMEMBER THE NAME OF THE FILE SO WE CAN DELETE IT LATER
mFileName = path;
}
public override void Close()
{
base.Close();
//ATTEMPT TO DELETE THE TEMP FILE
if (mFileName != null)
try
{
File.Delete(mFileName);
mFileName = null;
}
catch
{
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
//ATTEMPT TO DELETE THE TEMP FILE
if (disposing)
if (mFileName != null)
try
{
File.Delete(mFileName);
mFileName = null;
}
catch
{
}
}
}
Leave a Reply
You must be logged in to post a comment.
Michael E. Chancey Jr. Software Engineer Extraordinaire