I'm having trouble getting the MemoryMappedFile pattern to work. I have a directory with 25 excel spreadsheets that I want to load using MemoryMappedFile. The MemoryMappedFile.CreateFromFile works just fine, but when I call MemoryMappedFile.OpenExisting(lender), it reads the first 3 files, and then on the 4th attempt, it fails with exception:
Unable to find the specified file.
public static void Main()
{
var files = System.IO.Directory.EnumerateFiles(@"lendersheets", "*.xls*").ToList();
foreach (var file in files)
{
var lender = System.IO.Path.GetFileNameWithoutExtension(file);
MemoryMappedFile.CreateFromFile(file, FileMode.Open, lender);
}
foreach (var file in files)
{
var lender = System.IO.Path.GetFileNameWithoutExtension(file);
using (var mmfExisting = MemoryMappedFile.OpenExisting(lender))
{
var stream = mmfExisting.CreateViewStream();
if (stream.CanRead && stream.CanWrite)
{
LoadFromStream(stream);
}
}
}
}
UPDATE:
I have noticed that when I remove the LoadFromStream method call, the exception doesn't occur anymore. Here is the logic for LoadFromStream:
private static object LoadFromStream(Stream stream)
{
hssfwb = new HSSFWorkbook(stream);
evaluator = new HSSFFormulaEvaluator(hssfwb);
return hssfwb;
}