Path to font file selected via FontDiolog in C#

Posted on 2009-05-01
Last Modified: 2013-12-17

I am using the excellent font library called freetype to render fonts with OpenGL.  The freetype library requires access to the actual  font files (e.g. C:\WINDOWS\Fonts\ARIALBI.TTF - Arial Bold Italic).

I would like to use the standard FontDialog via .NET and C# to select the font I want freetype to render.  After I select my font with the standard FontDialog I can get the fontDialog.Font, but this gives me no information as to where the actual font file lives on the hard drive.  I need the full path to the font file representing my selection via the FontDialog so that I can pass the file name to the freetype library so that it can extract the font information to then be rendered with OpenGL.

Any idea how I can get the path to the actual font file that represents the selection made with the standard FontDialog?

Thanks you,

Question by:pergull
    LVL 21

    Expert Comment

    Well it may be easier to just include the font file in your project, maybe in your bin file so that incase the user doesn't have it available, then its it makes your program work on all machines...
    LVL 46

    Accepted Solution

    Hi pergull,

    Take a look at the below example. It makes use of code from here -> http:/Q_23337167.html


    using System.Runtime.InteropServices;
    using Microsoft.Win32; 
    class Form1
    Button1_Click(System.Object sender, System.EventArgs e)
            string FontFolder = EnvironmentFolders.GetPath(EnvironmentFolders.SpecialFolder.Fonts);
            FontDialog fd = new FontDialog();
            if (fd.ShowDialog == Windows.Forms.DialogResult.OK) {
                RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts\\");
                if (rk == null) {
                    Interaction.MsgBox("key not found");
                else {
                    string fontName = fd.Font.Name + " (TrueType)";
                    tbFontName.Text = fontName;
                    tbFontPath.Text = FontFolder + "\\" + rk.GetValue(fontName).ToString;
    public class EnvironmentFolders s
        public enum SpecialFolder 
            AdministrativeTools = 48, 
            //{user name}\Start Menu\Programs\Administrative Tools 
            ApplicationData = 26, 
            //{user name}\Application Data 
            CommonAdministrativeTools = 47, 
            //All Users\Start Menu\Programs\Administrative Tools 
            CommonApplicationData = 35, 
            //All Users\Application Data 
            CommonDesktopDirectory = 25, 
            //All Users\Desktop 
            CommonDocuments = 46, 
            //All Users\Documents 
            CommonFavorites = 31, 
            CommonNonLocalizedStartup = 30, 
            //non localized common startup 
            CommonPrograms = 23, 
            //All Users\Programs 
            CommonStartMenu = 22, 
            //All Users\Start Menu 
            CommonStartup = 24, 
            //All Users\Startup 
            CommonTemplates = 45, 
            //All Users\Templates 
            ControlPanel = 3, 
            //My Computer\Control Panel 
            Cookies = 33, 
            DesktopDirectory = 16, 
            //{user name}\Desktop 
            Favorites = 6, 
            //{user name}\Favorites 
            Fonts = 20, 
            History = 34, 
            InternetCache = 32, 
            LocalApplicationData = 28, 
            //{user name}\Local Settings\Application Data (non roaming) 
            MyDocuments = 5, 
            //My Documents 
            MyPictures = 39, 
            //C:\Program Files\My Pictures 
            NetworkShortcuts = 19, 
            //{user name}\nethood 
            NonLocalizedStartup = 29, 
            //non localized startup 
            Printers = 4, 
            //My Computer\Printers 
            PrintHood = 27, 
            //{user name}\PrintHood 
            ProgramFiles = 38, 
            //C:\Program Files 
            ProgramFilesCommon = 43, 
            //C:\Program Files\Common 
            Programs = 2, 
            //Start Menu\Programs 
            Recent = 8, 
            //{user name}\Recent 
            RecycleBin = 10, 
            //{desktop}\Recycle Bin 
            SendTo = 9, 
            //{user name}\SendTo 
            StartMenu = 11, 
            //{user name}\Start Menu 
            Startup = 7, 
            //Start Menu\Programs\Startup 
            System = 37, 
            Templates = 21, 
            UserProfile = 40, 
            Windows = 36 
        [DllImport("shfolder.dll", CharSet = CharSet.Auto)] 
        private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath); 
        /// <summary> 
        /// Get an environment folder path for Windows environment folders 
        /// </summary> 
        /// <returns>A string pointing to the special path</returns> 
        /// <remarks></remarks> 
        public static string GetPath(SpecialFolder folder) 
            StringBuilder lpszPath = new StringBuilder(260); 
            SHGetFolderPath(IntPtr.Zero, (int)folder, IntPtr.Zero, 0, lpszPath); 
            return lpszPath.ToString(); 

    Open in new window


    Author Closing Comment

    Thank you very much.  I see that all the magic is through the registry.  I know exactly how to proceed.  Thanks again!

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now