Installing and removing fonts using C#
March 26. 2009 10 Comments
In this article I am going to describe how to install a font when your application is installed on a user's machine. You can do this directly in the application code which I will demonstrate, but for completeness I will demonstrate how it could be done when creating setup files for your application in Visual Studio.
1. To install the font using a Windows Installer MSI:
First thing you need to to after you add a Setup Project is add the Fonts Folder. Do this by right clicking File System on Target Machine -> Add Special Folder -> Fonts Folder.
Add your font file (the one you want to install) into the Fonts Folder by right clicking the right hand panel -> Add -> File... You will notice that theRegister property of the font file is vsdrfFont. This will automatically register your font in Windows when your application installs. That's it!
2. To Install the font directly from your application using code:
This is also relatively simple but you need to use some external calls. To add fonts add the following to the class that will contain the method to install the font:
1. To install the font using a Windows Installer MSI:
First thing you need to to after you add a Setup Project is add the Fonts Folder. Do this by right clicking File System on Target Machine -> Add Special Folder -> Fonts Folder.
Add your font file (the one you want to install) into the Fonts Folder by right clicking the right hand panel -> Add -> File... You will notice that theRegister property of the font file is vsdrfFont. This will automatically register your font in Windows when your application installs. That's it!
2. To Install the font directly from your application using code:
This is also relatively simple but you need to use some external calls. To add fonts add the following to the class that will contain the method to install the font:
[DllImport("gdi32.dll", EntryPoint="AddFontResourceW", SetLastError=true)] public static extern int AddFontResource([In][MarshalAs(UnmanagedType.LPWStr)] string lpFileName);To remove fonts add the following to the class that will contain the method to uninstall the font:
[DllImport("gdi32.dll", EntryPoint="RemoveFontResourceW", SetLastError=true)] public static extern int RemoveFontResource([In][MarshalAs(UnmanagedType.LPWStr)] string lpFileName);These methods can then easily be called in any method of the class, all you need to pass in is the full path of the font. A return value of 1 means the operation was successful, anything else means there was a problem. Here is some example code:
using System; using System.Runtime.InteropServices; using System.ComponentModel; namespace InstallMyFontExample { class Program { static void Main(string[] args) { int result = -1; int error = 0; // Try remove the font. result = RemoveFontResource(@"C:\MY_FONT_LOCATION\MY_NEW_FONT.TTF"); error = Marshal.GetLastWin32Error(); if (error != 0) { Console.WriteLine(new Win32Exception(error).Message); } else { Console.WriteLine((result == 0) ? "Font was not found." : "Font removed successfully."); } // Try install the font. result = AddFontResource(@"C:\MY_FONT_LOCATION\MY_NEW_FONT.TTF"); error = Marshal.GetLastWin32Error(); if (error != 0) { Console.WriteLine(new Win32Exception(error).Message); } else { Console.WriteLine((result == 0) ? "Font is already installed." : "Font installed successfully."); } Console.ReadKey(); } } }
And that's all there is to it.
NOTE: You may not see the font appear in the Windows Fonts directory however the font is really installed, just open up Word and check the font list.
UPDATE: Quite a few developers are using this font installation code when and having problems using the font in their own applications immediately afterward. You should use the code above if you are installing a font that you want other applications to use. If you just want to use a custom font in your own app, there is no need to install it. Here is some code you can use to assign a custom font to a label without actually installing it first:
var pfc = new PrivateFontCollection(); pfc.AddFontFile(@"C:\MyFont.ttf"); myLabel.Font = new Font(pfc.Families[0], 14, FontStyle.Regular);