views:

52

answers:

3

I'm kind of shooting in the dark here - searches don't seem to be productive on this. I want to create a .NET program to monitor the install and uninstall of fonts. If something is installed or uninstalled, I want to run a program to do something.

I thought maybe a Windows Service, but haven't gotten very far with figuring out how to monitor fonts. Is this the right way and if so, what am I overlooking? If not, is there a different way to do this?

+1  A: 

you can create a hook for the WM_FONTCHANGE windows message.

check this article Using Window Messages to Implement Global System Hooks in C#

RRUZ
Does Windows send out this message on install/uninstall? Or, I guess more accurately, does Windows use `AddFontResource` or `RemoveFontResource`?
Otaku
@Otaku, Check the Msdn Link from the answer `An application that adds or removes fonts from the system (for example, by using the AddFontResource or RemoveFontResource function) should send this message to all top-level windows.`
RRUZ
I did. Unfortunately, `AddFontResource` does not install a font permanently. From http://msdn.microsoft.com/en-us/library/dd183326(VS.85).aspx it states *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 monitoring font additions this way wouldn't reveal which fonts have actually been installed.
Otaku
I don't get the problem. If the system restarts then you'll have to enumerate the fonts again anyway.
Hans Passant
+2  A: 

You could monitor the registry for font changes. Looking at the process monitor I see it stores the information here on windows 7.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts

Jason Rowe
What would this entail? Running a query against that key every second to see if the `.Count` has changed?
Otaku
If the application is running getting an event would be best. Then after getting the event you can look through the registry to check for changes. On start up you could enumerate the fonts in the registry to check for changes also.
Jason Rowe
A: 

I don't know that much about Fonts so I might be mistaken but I think that they're all installed in %windir%\Fonts.

If that is the case you should be able to set up a FileSystemWatcher in your service to watch that directory for any changes.

ho1
Most are, but not all (Vista/7 allow for fonts to be installed outside of %windir%\Fonts).
Otaku