How to install a windows font using C#

14,787

Solution 1

You'll need a different approach installing fonts.

  • Use an installer (create a setup project) to install the fonts
  • Another (more easy) approach using a native method.

Declare the dll import:

    [DllImport("gdi32.dll", EntryPoint="AddFontResourceW", SetLastError=true)]
    public static extern int AddFontResource(
        [In][MarshalAs(UnmanagedType.LPWStr)]
        string lpFileName);

In your code:

    // Try install the font.
    result = AddFontResource(@"C:\MY_FONT_LOCATION\MY_NEW_FONT.TTF");
    error = Marshal.GetLastWin32Error();

The source:

http://www.brutaldev.com/post/2009/03/26/Installing-and-removing-fonts-using-C

I put it together in a unit test, I hope that helps:

[TestFixture]
public class Tests
{
    // Declaring a dll import is nothing more than copy/pasting the next method declaration in your code. 
    // You can call the method from your own code, that way you can call native 
    // methods, in this case, install a font into windows.
    [DllImport("gdi32.dll", EntryPoint = "AddFontResourceW", SetLastError = true)]
    public static extern int AddFontResource([In][MarshalAs(UnmanagedType.LPWStr)]
                                     string lpFileName);

    // This is a unit test sample, which just executes the native method and shows
    // you how to handle the result and get a potential error.
    [Test]
    public void InstallFont()
    {
        // Try install the font.
        var result = AddFontResource(@"C:\MY_FONT_LOCATION\MY_NEW_FONT.TTF");
        var error = Marshal.GetLastWin32Error();
        if (error != 0)
        {
            Console.WriteLine(new Win32Exception(error).Message);
        }
    }
}

That should help you on your way :)

Solution 2

   internal static void InstalarFuente(string NombreFnt,string RutaFnt)
    {
        string CMD = string.Format("copy /Y \"{0}\" \"%WINDIR%\\Fonts\" ", RutaFnt);
        EjecutarCMD(CMD);

        System.IO.FileInfo FInfo = new System.IO.FileInfo(RutaFnt);
        CMD = string.Format("reg add \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts\" /v \"{0}\" /t REG_SZ /d {1} /f", NombreFnt, FInfo.Name);
        EjecutarCMD(CMD);
    }

    public static void EjecutarCMD(string Comando)
    {
        System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo("cmd.exe");
        Info.Arguments = string.Format("/c {0}", Comando);
        Info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        System.Diagnostics.Process.Start(Info);
    }
Share:
14,787
Shahin Rahbar
Author by

Shahin Rahbar

Updated on July 24, 2022

Comments

  • Shahin Rahbar
    Shahin Rahbar almost 2 years

    How can I install a font using C#?

    I tried copying the fonts using File.Copy() but I am not allowed due to access rights limitations (UnauthorizedException).

    What should I do?

  • Shahin Rahbar
    Shahin Rahbar about 11 years
    Can you explain more about "Declare the dll import:" bcuz i'm new to c#
  • bas
    bas about 11 years
    @ShahinRahbar did you succeed yet? You didn't have to accept the answer upfront. Just upvote the answer if you are happy with it, accept the answer when your question is actually solved. Just let me know when you got it, if not, i'll try to help
  • Shahin Rahbar
    Shahin Rahbar about 11 years
    i dont have rep for upvote... it compile correctly but font doesnt install
  • Shahin Rahbar
    Shahin Rahbar about 11 years
    The system cannot find the file Specified
  • bas
    bas about 11 years
    Maybe this is the time to create a new question where you share the appropriate amount of information so that the SO community can help you. Copy/paste the code you have right now and share the error + stacktrace in a new question
  • JxDarkAngel
    JxDarkAngel over 9 years
    Logoff to take into account the Font
  • Lee
    Lee almost 9 years
    Can anyone help with the error: "Invalid menu handle"
  • bas
    bas almost 9 years
    @user2816736 Some context might help, and if you have a specific question, ask a new question iso comment on solved question
  • S.H.
    S.H. about 2 years
    Bravo! Bravissimo! By tests in this case.
  • S.H.
    S.H. about 2 years
    Excuse me, do you know about "more than user" rights for copy file into Windows dir?