November 10, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

Installing and removing fonts using C#

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.

FontInstall-CreateFontsFolder

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 the Register 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.

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.

FontInstall-CreateFontsFolder

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 the Register 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.