1

I'm having trouble determining if a custom font (Euclid Triangle) is installed on a collection of machines.

I've used the code listed here "Test if a Font is installed" and it works on my Windows 10 machine. But it does not work on a Windows 7 machine and a bunch of machines at my customer.

All machines have .Net 4.5 and above.

The font is not listed if I try listing all the Fonts on the machine:

    static void ListFonts()
    {
        try
        {
            using (InstalledFontCollection fontsCollection = new InstalledFontCollection())
            {
                FontFamily[] fontFamilies = fontsCollection.Families;
                var fonts = new List<string>();
                foreach (FontFamily font in fontFamilies)
                    fonts.Add(font.Name);
                var file = new FileInfo(Assembly.GetExecutingAssembly().Path() + "\\fonts.txt");
                Serializer.SerializeToFile(fonts, file.FullName);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Printer Configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);
            var file = new FileInfo(Assembly.GetExecutingAssembly().Path() + "\\log.txt");
            File.WriteAllText(file.FullName, ex.ToString());
            Console.WriteLine(ex.ToString());
        }
    }

EDIT: I've run my code as Administrator to confirm that the issue is not related to permissions.

Matt Fitzmaurice
  • 1,319
  • 4
  • 19
  • 40
  • Just guessing: insufficient rights on other machines? – Alexander Schmidt Oct 26 '18 at 06:41
  • 1
    Your code does not search for a particular font, it only adds them to a list, and writes it to a file… – dumetrulo Oct 26 '18 at 10:35
  • I know the code that I listed doesn't search for a particular font.. The post that i link to contains numerous examples of that - i have tried them all and none work on the target machines. – Matt Fitzmaurice Oct 28 '18 at 23:52
  • 1
    So if I read your edited post correctly, you say the font is installed (i.e. can be used from, say, WordPad) but does not show when you list the fonts using the code? – dumetrulo Oct 29 '18 at 08:45
  • Correct. The font appears in MS Word etc and can be used. The font cannot be found when listing the fonts via code. – Matt Fitzmaurice Oct 29 '18 at 21:50

1 Answers1

1

Using LINQ, iterating the list of installed fonts and checking whether it contains a particular one should be essentially a one-liner (plus the inevitable boilerplate):

static bool IsFontInstalled(string fontname)
{
    using (var ifc = new InstalledFontCollection())
    {
        return ifc.Families.Any(f => f.Name == fontname);
    }
}
dumetrulo
  • 1,993
  • 9
  • 11