We create a large count of fonts for a short use. The fonts are embedded in documents. I want delete the font files if not use anymore. How can we do this? The follow simplified code does not work:
PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile(fontFile);
FontFamily family = pfc.Families[0];
Console.WriteLine(family.GetName(0));
family.Dispose();
pfc.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
File.Delete(fontFile);
The delete of the file is failing because the file is locked. What can I do else to free the file lock?
PS: Before we have use AddMemoryFont. This work with Windows 7. But with Windows 8 .NET use the wrong font files after the first FontFamily was Disposed. Because every Document can contain other fonts we need a very large count of fonts and can not hold references to all.