We're replacing an "old" web application (the original CodeCentral was written over 10 years ago, now running at http://cc.embarcadero.com with over 12.7 million served) that uses a file based cache for serving downloads. The web application manages this file cache.
The only complicated bit of using file-based downloads is updating a file that might be in use by another version. Back when I wrote the the original Delphi CGI version of CodeCentral, I vaguely recall being able to rename the existing file even though someone might be downloading it, writing the new file with the correct name, and eventually cleaning up the old file.
However, I may be delusional, because that's not working with our current IIS 6 and ASP.NET 2.x or 3.x code. So, what we've done instead is this, which is quite kludgy but eventually works:
public static void FileCreate(Stream AStream, string AFileName, int AWaitInterval)
{
DateTime until = DateTime.Now.AddSeconds(AWaitInterval);
bool written = false;
while (!written && (until > DateTime.Now))
{
try
{
using (FileStream fs = new FileStream(AFileName, FileMode.Create))
{
CopyStream(AStream, fs);
written = true;
}
}
catch (IOException)
{
Thread.Sleep(500);
}
}
}
I'm thinking we're missing something simple and there's got to be a better way to replace a file currently being downloaded via IIS and ASP.NET. Is there?