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

13

我正在使用C#获取系统字体文件夹的完整路径,但找不到相应的类或dll。

4个回答

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

注意,在SpecialFolder枚举中的Fonts文件夹仅适用于 .Net 4 及以上版本。


30

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

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

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

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

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

  3. 将Fonts名称连接到Windows文件夹上以获得最终结果。

此代码示例使用了系统文件夹并执行了此操作。 您可以选择其他文件夹。

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"

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

5
Environment.SpecialFolders.Fonts

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接