简体   繁体   中英

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...

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.Families.Any(b => b.Name == a.Name)))
{
   Console.WriteLine($"'{item.Name}' already installed");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM