private static DataSet GetTempFileDataSet()
{
DataSet dataSet = null;
var masterFile = new FileInfo(Manager.BinaryFilePath);
if (masterFile.Exists)
{
// Get the publish date of the master data file.
var masterDate = GetDataFileDate(masterFile.FullName);
// Make sure the temporary directory exists.
var directory = new DirectoryInfo(
Mobile.Configuration.Support.GetFilePath(Constants.TemporaryFilePath));
if (directory.Exists == false)
{
directory.Create();
}
// Loop through the temporary files to see if we can use any of them.
foreach (var temporaryFile in directory.GetFiles("*.tmp").Where(i =>
i.Exists &&
i.FullName.Equals(masterFile.FullName) == false &&
i.Name.StartsWith(masterFile.Name) &&
masterDate.Equals(GetDataFileDate(i.FullName))))
{
// Use the existing temporary file.
EventLog.Debug(
"Trying to use existing temporary data file '{0}' with published date '{1}' as data source.",
temporaryFile.FullName,
masterDate.HasValue ? masterDate.Value.ToShortDateString() : "null");
try
{
// Try and create the data set from the existing temporary file.
// If the file can't be used then record the exception in debug
// logging.
dataSet = StreamFactory.Create(temporaryFile.FullName, File.GetLastWriteTimeUtc(masterFile.FullName), true);
// The data set could be created so exit the loop.
break;
}
catch (Exception ex)
{
// This can happen if the data file is being used by another process in
// exclusive mode and as yet hasn't been freed up.
EventLog.Debug(
"Could not use existing temporary data file '{0}' as data source",
temporaryFile.FullName);
EventLog.Debug(ex);
dataSet = null;
}
}
if (dataSet == null)
{
// No suitable temp file was found, create one in the
// temporary file folder to enable the source file to be updated
// without stopping the web site.
dataSet = StreamFactory.Create(CreateNewTemporaryFile(masterFile).FullName, File.GetLastWriteTimeUtc(masterFile.FullName), true);
}
}
return dataSet;
}