private static LicenceKeyResults ActivateDownloadedFile(WebClient client, string uncompressedTempFile)
{
var status = LicenceKeyResults.Success;
// Rename the current master file to a temp file so enable the new
// master file to take it's place and to rollback if there's a problem.
var tempCopyofCurrentMaster = String.Format("{0}.tmp", BinaryFile.FullName);
try
{
// Both the MD5 hash was good and the provider was created.
// Save the data and force the factory to reload.
if (BinaryFile.Exists)
{
// Keep a copy of the old data in case we need to go back to it.
File.Move(BinaryFile.FullName, tempCopyofCurrentMaster);
}
// Copy the new file to the master file.
File.Move(uncompressedTempFile, BinaryFile.FullName);
// Get the published date from the new data file.
var publishedDate = WebProvider.GetDataFileDate(BinaryFile.FullName);
// Sets the last modified time of the file downloaded to the one
// provided in the HTTP header, or if not valid then the published
// date of the data set.
DateTime lastModified;
if (DateTime.TryParseExact(
client.ResponseHeaders[HttpResponseHeader.LastModified],
"R",
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal,
out lastModified) == false)
{
lastModified = publishedDate.Value;
}
BinaryFile.LastWriteTimeUtc = lastModified.ToUniversalTime();
EventLog.Info(String.Format(
"Automatically updated binary data file '{0}' with version " +
"published on the '{1:d}'.",
BinaryFile.FullName,
publishedDate));
}
catch (Exception ex)
{
if (BinaryFile.Exists == false)
{
File.Move(tempCopyofCurrentMaster, BinaryFile.FullName);
}
EventLog.Warn(ex);
status = LicenceKeyResults.WriteDataFile;
}
finally
{
File.Delete(tempCopyofCurrentMaster);
}
return(status);
}