开发者

How to get the path to the Windows fonts folder?

开发者 https://www.devze.com 2023-04-04 10:47 出处:网络
I\'m using C# to get the exact path of the system\'s fonts folder. Couldn\'t find which class/dll does it.开发者_高级运维string fontsfolder = System.Environment.GetFolderPath(

I'm using C# to get the exact path of the system's fonts folder. Couldn't find which class/dll does it.开发者_高级运维


string fontsfolder = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Fonts);

Note that the Fonts folder in the SpecialFolder enumeration is only available in .Net 4 and beyond.


For the answers here that specify Environment.SpecialFolders.Fonts, that enumeration value only exists in .NET 4.0+.

For .NET 1.1 - 3.5 you can do the following:

The Fonts folder is inside the Windows folder (e.g. C:\Windows\Fonts). Programmatically grab it through these steps:

  1. Key off a different special folder that does exist in the enumeration value of .NET 2, like the system folder Environment.SpecialFolder.System.

  2. Grab the parent folder of the system folder (gets the base Windows folder)

  3. Concatenate the Fonts name onto the Windows folder to get the final result.

This code sample uses the System folder and does it. There are other folders you can key off.

using System.IO;

// get parent of System folder to have Windows folder
DirectoryInfo dirWindowsFolder = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System));

// Concatenate Fonts folder onto Windows folder.
string strFontsFolder = Path.Combine(dirWindowsFolder.FullName, "Fonts");

// Results in full path e.g. "C:\Windows\Fonts"


string fontFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);


Environment.SpecialFolders.Fonts
0

精彩评论

暂无评论...
验证码 换一张
取 消