Home > Mobile >  Compare two font collections
Compare two font collections

Time:11-05

I'm trying to compare two font collections in order to understand which fonts are already installed and which not.

Code is as follows:

var workingdir = new DirectoryInfo(Path.Combine(basepath, directory));
InstalledFontCollection col = new InstalledFontCollection();
PrivateFontCollection pcol = new PrivateFontCollection();

foreach (FileInfo fontname in workingdir.GetFiles("*.ttf"))
{
    pcol.AddFontFile(fontname.FullName);
}
foreach (var item in pcol.Families)
{
    if (col.Families.Contains(item))
    {
        Console.WriteLine(item.Name   " already installed");
    }
    else
    {
        Console.WriteLine(item.Name   " NOT INSTALLED");
    }
}

Problem is that I know for sure that inside my workingdir there are some fonts already installed and some not, but the console output shows me that EVERY fontfile is not installed. What am I missing? I guess there's something wrong in my logic but I don't understand where is the problem...

CodePudding user response:

Contains checks if it is the same object with ==, but you have to check for the Names to be the same.

var workingdir = new DirectoryInfo(Path.Combine(basepath, directory));
var col = new InstalledFontCollection();
var pcol = new PrivateFontCollection();

foreach (var fontname in workingdir.GetFiles("*.ttf"))
{
    pcol.AddFontFile(fontname.FullName);
}

foreach(var item in pcol.Families.Where(a => col.Any(b => b.Name == a.Name)))
{
   Console.WriteLine($"'{item.Name}' already installed");
}
  • Related