Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compare two font collections

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…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading