0

I need to get the path of a specific font in my c:/windows/ folder The below code works perfectly when the target framework is 4.0 But my application can target only 3.5 and i need to use this in a console application c#

How can i achieve this ? Thanks.

string arialuniTff = path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts), "arial.TTF");

Error Msg in 3.5 : 'System.Environment.SpecialFolder' does not contain a definition

Anuya
  • 8,082
  • 49
  • 137
  • 222

2 Answers2

4

The fonts folder is typically located at %windir%\Fonts, so you should be able to get the location like this:

Path.Combine(
    System.Environment.GetEnvironmentVariable("windir"),
    "Fonts");

It is a virtual folder, so in theory it could be located somewhere else. In practice, I've never seen that happen or heard of it happening. (Microsoft is confident enough in this location to reference it on their "how to install a font" page). I'm sure that if you're trying to locate a specific file name like that you have good error handling already, though.

Bonus information:

You might know this already, but if you need to know what classes, methods, etc. are available in a specific version of the .net framework, you can find out from MSDN. Go to the documentation page (say this one on Environment.SpecialFolder), and click on the ".NET Framework 4.5" link in the top left corner and choose a different version to see the page you are looking at as it was in that version.

andypaxo
  • 6,171
  • 3
  • 38
  • 53
  • I am getting the similar error message now. Error : 'System.Environment.SpecialFolder' does not contain a definition for 'Windows' – Anuya Nov 28 '12 at 04:55
  • Oh, that was dumb of me. I've replaced one 4.5 specific call with another that's also 4.5 specific. Hang on a minute, and I'll find an alternative. – andypaxo Nov 28 '12 at 05:04
  • There. That should work in any framework all the way back to 2.0! – andypaxo Nov 28 '12 at 05:08
0

Please refer How to get the path to CSIDL_COMMON_DOCUMENTS in .NET 3.5?

It provides the location for const int CSIDL_COMMON_DOCUMENTS = 0x002e;.

For Fonts folder, use const int CSIDL_FONTS = 0x0014;

It would be:

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

const int CSIDL_FONTS = 0x0014;
const int CSIDL_FLAG_CREATE = 0x8000;

StringBuilder sb = new StringBuilder();

int retVal = SHGetFolderPath(IntPtr.Zero,
                                 CSIDL_FONTS | CSIDL_FLAG_CREATE,
                                 IntPtr.Zero,
                                 0,
                                 sb);
Debug.Assert(retVal >= 0);  // assert that the function call succeeded
String folderLocation = sb.ToString();
Community
  • 1
  • 1
Amber Beriwal
  • 1,568
  • 16
  • 30