C# FontFamily not showing new fonts

2019-07-04 02:33发布

问题:

I notice that when we are trying to list fonts using C#, that it works fine; however, if we are to install a new font while the application is running, calling the enumeration of fonts doesn't return the new font, until the application is restarted.

Here's the code:

public void Populate(bool b)
{
    both = b;
    foreach (FontFamily ff in FontFamily.Families)
    {
        if(ff.IsStyleAvailable(FontStyle.Regular))
            Items.Add(ff.Name);                                             
    }           
}

Notes for the above method: Items.Add() is adding items to a comboBox.

I must be understanding something incorrectly here. How can i get the above code to requery the system for the fonts, even the new ones?

回答1:

Did you try with

using System.Drawing.Text;
InstalledFontCollection fonts = new InstalledFontCollection();
foreach (FontFamily ff in fonts.Families)
{
    if (ff.IsStyleAvailable(FontStyle.Regular))
        Items.Add(ff.Name);
}


回答2:

public void Populate(bool b)
{
    both = b;
    InstalledFontCollection fonts = new InstalledFontCollection();
    foreach (FontFamily ff in fonts.Families)
    {
        if (ff.IsStyleAvailable(FontStyle.Regular))
            Items.Add(ff.Name);
    }

}