Share via

how to use the font I add to the system?

mc 7,186 Reputation points
2026-06-04T14:14:46.45+00:00

I am using winforms

I copy the font file to Fonts folder in windows server 2022 and I can not use it.

var font =new Font("MyFont"); the font is Microsoft sans serif not my font.

why?

if the name is not right how to get the name ?

Developer technologies | Windows Forms
0 comments No comments

Answer accepted by question author

Nancy Vo (WICLOUD CORPORATION) 5,705 Reputation points Microsoft External Staff Moderator
2026-06-05T03:06:04.8633333+00:00

Hello @mc ,

Thanks for your question.

Simply copying the font file into the C:\Windows\Fonts folder is not enough to make it available to your application. Windows needs to properly register the font. On servers (especially Windows Server 2022), this often doesn't happen reliably.

I recommend using PrivateFontCollection, which loads the font directly from the file.

  1. Add the font file to your project:
  • Create a folder called Fonts in your WinForms project.
  • Copy your .ttf (or .otf) file into this folder.
  • In Solution Explorer, right-click the font file → Properties → Set Copy to Output Directory to Copy if newer.
  1. Find the correct font family name:
  • Double-click your font file on your computer.
  • In the preview window, look at the big name at the top.
  • You can also check the Details tab in file Properties.
  1. You can refer to following example code:
using System.Drawing;
using System.Drawing.Text;
using System.IO;

public partial class YourForm : Form
{
    private PrivateFontCollection privateFonts = new PrivateFontCollection();

    public YourForm()
    {
        InitializeComponent();
        LoadCustomFont();
    }

    private void LoadCustomFont()
    {
        try
        {
            string fontPath = Path.Combine(Application.StartupPath, "Fonts", "MyCustomFont.ttf");

            if (File.Exists(fontPath))
            {
                privateFonts.AddFontFile(fontPath);
            }
            else
            {
                MessageBox.Show("Font file not found!");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Failed to load font: " + ex.Message);
        }
    }

    private void ApplyCustomFont(Control control, float size = 12f, FontStyle style = FontStyle.Regular)
    {
        if (privateFonts.Families.Length > 0)
        {
            FontFamily family = privateFonts.Families[0];
            control.Font = new Font(family, size, style);
        }
    }

    private void Form_Load(object sender, EventArgs e)
    {
        ApplyCustomFont(label1, 14f);
        ApplyCustomFont(button1, 12f);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            privateFonts?.Dispose();
        }
        base.Dispose(disposing);
    }
}

Note: please replace "MyCustomFont.ttf" with your actual filename.

I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AI answer

    2026-06-04T14:15:12.0633333+00:00

    Windows Forms can only use fonts that are properly installed in Windows. Simply copying the font file into C:\Windows\Fonts (or the Fonts folder on Server 2022) is not always enough; Windows must register the font.

    1. Ensure the font is installed correctly

    Use one of these supported methods so the font is registered in the system font table:

    • Right-click the .ttf or .otf file and select Install (or Install for all users).
    • Or copy the font file into %windir%\Fonts and then either:

    If the font is not installed/registered, Windows Forms will fall back to Microsoft Sans Serif.

    1. Why new Font("MyFont") gives Microsoft Sans Serif

    Windows Forms supports TrueType and limited OpenType fonts. If the requested font:

    • Is not installed,
    • Is not supported, or
    • The name does not match an installed font family,

    then Windows Forms substitutes Microsoft Sans Serif.

    This behavior is documented: if a font is not supported or not installed, “the Microsoft Sans Serif font will be substituted.”

    1. How to get the correct font name

    The string passed to new Font("...") must match the font family name as Windows knows it, not necessarily the file name.

    To discover the correct name:

    • After installing the font, open any app that uses the Windows font list (for example, Word or another WinForms control) and check the font dropdown; use that exact family name.
    • Or enumerate fonts programmatically and inspect their FontFamily.Name.

    Once the correct family name is known and the font is installed, creating it in WinForms looks like:

    var font = new Font("CorrectFamilyName", 12f);
    

    If the font is still not found or supported, Windows Forms will continue to substitute Microsoft Sans Serif.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.