Is there an easy way (in .Net) to test if a Font is installed on the current machine?
Other answers proposed using Font
creation only work if the FontStyle.Regular
is available. Some fonts, for example Verlag Bold, do not have a regular style. Creation would fail with exception Font 'Verlag Bold' does not support style 'Regular'. You'll need to check for styles that your application will require. A solution follows:
public static bool IsFontInstalled(string fontName)
{
bool installed = IsFontInstalled(fontName, FontStyle.Regular);
if (!installed) { installed = IsFontInstalled(fontName, FontStyle.Bold); }
if (!installed) { installed = IsFontInstalled(fontName, FontStyle.Italic); }
return installed;
}
public static bool IsFontInstalled(string fontName, FontStyle style)
{
bool installed = false;
const float emSize = 8.0f;
try
{
using (var testFont = new Font(fontName, emSize, style))
{
installed = (0 == string.Compare(fontName, testFont.Name, StringComparison.InvariantCultureIgnoreCase));
}
}
catch
{
}
return installed;
}