Detecting the presence of a given font in .NET

Maybe this seems obvious to other people, but I couldn’t find anything in Google on it. Maybe I didn’t know the right keywords to search for.

At any rate, lets say you have an application that’s optimized for a specific font — but you can’t guarantee that font is installed. You might want to, as a contingency, fall-back to a different font — maybe one that isn’t quite as attractive, but that you know for sure will be present. This other font might have slightly different sizing, so in the case where you fall back, you might want to tweak your UI widgets, or alignment margins and widths, to look better if the fall-back is used. Here’s my solution (C#)…

Font fnt = new Font("GoodLookinFont", 10);
if (fnt.FontFamily.Name != "GoodLookinFont")
//adjust display for fall-back font

What happens is simple: .Net attempts to create a Font object using your preferred font. If it can’t find that font on the local system, it will instead create the Font object using “Microsoft Sans Serif.” If the Font object’s FontFamily Name is not what you expect, you know you’ll have to adjust your display.