Welcome to Atalasoft Community Sign in | Help

Getting the fonts folder

Here's how to get the fonts folder on a windows system incorrectly:

string fontPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\Fonts";

It looks so terse and tempting - the SpecialFolder enum takes you all the way there except for "\Fonts", so you just tack that on.

OK - first off, you shouldn't concat path element strings - that's what Path.Combine is for.  Secondly, just don't do this.  Is the Fonts folder named "Fonts" on a system in France?  Turkey? Cambodia?  Yeah, didn't think so.

Since SpecialFolder is missing the specifier, you have you implement it yourself:

[DllImport("shell32.dll")]
private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken,
           uint dwFlags, [Out] StringBuilder pszPath);

public static string GetFontFolderPath()
{
    StringBuilder sb = new StringBuilder();
    SHGetFolderPath(IntPtr.Zero, 0x0014, IntPtr.Zero, 0x0000, sb);

    return sb.ToString();
}

 0x0014 is a constant that tell SHGetFolderPath to look in the right place.  If you're running on Vista, you should use SHGetKnownFolderPath instead.
 

Published Monday, August 25, 2008 3:49 PM by Steve Hawley

Comments

Wednesday, September 03, 2008 10:27 AM by DotNetKicks.com

# What if it's not in Environment.SpecialFolder?

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Anonymous comments are disabled