Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want install my font on windows by c# coding. I use below code to do this, it run without error but i can't see my font in c:/windows/fonts.
What should i do?
My font stored in Resource of project.
My code:

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

 AddFontResource("B Homa Regular.ttf");
Posted
Updated 30-Jun-15 22:20pm
v2

The first thing to do is check the return value.
The documentation[^] says:
If the function succeeds, the return value specifies the number of fonts added.
If the function fails, the return value is zero. No extended error information is available.
So start by looking to see if it returns zero or one.
However, it also says:
"Any application that adds or removes fonts from the system font table should notify other windows of the change by sending a WM_FONTCHANGE message to all top-level windows in the operating system. The application should send this message by calling the SendMessage function and setting the hwnd parameter to HWND_BROADCAST.
When an application no longer needs a font resource that it loaded by calling the AddFontResource function, it must remove that resource by calling the RemoveFontResource function.
This function installs the font only for the current session. When the system restarts, the font will not be present. To have the font installed even after restarting the system, the font must be listed in the registry."

So you don't just call the add a font method, you need to notify other apps, and almost certainly add it to the registry.
 
Share this answer
 
Comments
Rasoul Takalloo 1-Jul-15 5:40am    
Thanks OriginalGriff. It returns one(of course i don't see my font in c:\windows\fonts) and I'm not going to remove it. So, remains one issue:list in registry. how? have you some sample?
OriginalGriff 1-Jul-15 5:56am    
No, I've never needed to.
But...google has found this:
http://stackoverflow.com/questions/21986744/how-to-install-a-font-programatically-c

Which shows the registry code. Note: you are probably going to need UAE to do this (understandably!)
Rasoul Takalloo 1-Jul-15 6:55am    
Goooood, I saw this link before. it has one error in its syntax that by solving can be useful.I write true solution in next comment. thank OriginalGriff
Note that your font source must add to Resource of project

C#
File.Copy("yourfont.ttf", Path.Combine("C:\\Windows", "Fonts" + "yourfont.ttf"),true);

RegistryKey fontkey= Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts");

fontkey.SetValue("yourfont", "yourfont.ttf");
fontkey.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900