如何获取Windows字体文件夹的路径?

时间:2011-09-13 19:58:21

标签: c# .net fonts path directory

我正在使用C#来获取系统字体文件夹的确切路径。 找不到哪个类/ dll。

4 个答案:

答案 0 :(得分:36)

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

请注意,SpecialFolder枚举中的Fonts文件夹仅在.Net 4及更高版本中可用。

答案 1 :(得分:28)

对于此处指定Environment.SpecialFolders.Fonts的答案,该枚举值仅存在于.NET 4.0 +中。

对于.NET 1.1 - 3.5,您可以执行以下操作:

Fonts文件夹位于Windows文件夹中(例如C:\ Windows \ Fonts)。通过以下步骤以编程方式获取它:

  1. 键入.NET 2的枚举值中存在的其他特殊文件夹,如系统文件夹Environment.SpecialFolder.System

  2. 抓取系统文件夹的父文件夹(获取基本Windows文件夹)

  3. 将字体名称连接到Windows文件夹以获取最终结果。

  4. 此代码示例使用System文件夹并执行此操作。您可以关闭其他文件夹。

    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"
    

答案 2 :(得分:7)

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

答案 3 :(得分:5)

Environment.SpecialFolders.Fonts