How to get font version (or filename) in my program

  • Thread starter Thread starter thoatson
  • Start date Start date
T

thoatson

Guest
I am working on the installer for a program which provides data in several minority languages. In the installer, I want to have a custom action (written in C#) to check if a needed font is already installed (perhaps from a prior installation). If so, the custom action should check the version of the installed font to see if it should be upgraded by a font in our installation package.

Sounds straightforward, but...

1. I used code like the following to see if the font is already installed:

using (FontFamily family = new FontFamily(fontFamilyName))
result = family.IsStyleAvailable(fontStyle);

But I can't get the version information from the font family, so I need to do something else. A friend suggested I use the Windows SDK function GetFontData(). But I don't know how to get access to this from within C# code. Is there a C# library that has a wrapper for this functionality?

I also saw this suggestion:

var segoe = new FontFamily("Segoe UI");
var typeface = new Typeface(segoe, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
var isGlyphTypeface = typeface.TryGetGlyphTypeface(out var glyph);
if (isGlyphTypeface)
{
Console.WriteLine(glyph.Version);
}
But when I tried it, I could not find Typeface as the name of a known class.


2. If that is not possible, I would open the font file to read the version directly from the "Names" table, but that assumes I can find the name and location of the installed font file. (Fonts can be installed either for a single user or for all users, causing them to be installed to different locations.) I already have code to obtain the version from the Names table (which I would need for solution #1 also.) Searching the forums, I have found that this is not straightforward, either. One suggestion was to use:

Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Fonts))

However, this seems to only address a single font location. Another suggestion was to use:

fonts = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\Fonts", false);
if (fonts == null)
{
fonts = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Fonts", false);
if (fonts == null)
{
throw new Exception("Can't find font registry database.");
}
}

However, I'm concerned about relying on the hard-coded keys shown here, as the key may vary with different versions of Windows. Any other suggestions? (There is a bit of C++ code on codeproject, but I would like to do this in C#, if possible. Seems the conversion could be non-trivial... Also, I saw some comments about bugs in it.)

Thanks in advance for any help you can offer.

Continue reading...
 
Back
Top