Installing Fonts programatically C#

  • Thread starter Thread starter ganesh
  • Start date Start date
G

ganesh

I have a piece of C# code using ShellExecute API to install a TrueType
font on Windows XP. The font is installed correctly and is visible
with programs like WinWord. However, the font file seems to be locked
down and cannot be deleted by administrators, unless the machine is
rebooted.

The code is as below:

class Program
{
[DllImport("shell32.dll", EntryPoint = "ShellExecute")]
public static extern int ShellExecuteA(int hwnd, string
lpOperation, string lpFile, string lpParameters, string lpDirectory,
int nShowCmd);

static void Main()
{
string[] arr = Environment.GetCommandLineArgs();
String FontPath = arr[1];
ShellExecuteA(0, "open", FontPath, "", "", 0);
}
}

Any ideas what I am missing?
 
It looks like you're opening the font, not installing it... I'm not sure...
 
ganesh,

You are not installing the font correctly.

In order to install the font to the system, you need to call the
AddFontResourceEx or AddFontResource API function through the P/Invoke
layer.
 
Nick,

Thanks for the suggestion.

I redid my code using AddFontResource API, but the result is the same
as with ShellExecute. The font gets installed in both cases, but the
font file itself seems to be locked and cannot be deleted, unless the
machine is rebooted. Below is the modified code using AddFontResource
API:

class Program
{
[DllImport("gdi32", EntryPoint = "AddFontResource")]
public static extern int AddFontResourceA(string lpFileName);

static void Main()
{
string[] arr = Environment.GetCommandLineArgs();
String FontPath = arr[1];
AddFontResourceA(DestinationFile);
}
}
 
Ganesh,

This might be a stupid question, but you said that you run programs
after that use the font. Are the programs that are using the font still
running when you try and delete the font? If they are, then I can see that
would be the reason why they are not deleting, because they are in use.
 
Nick

I ran the applications like Winword to verify if the font is
recongnized. However, the font file is locked up even prior to running
any application.

Thanks
Ganesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top