【问题标题】:How to test if a Font is installed?如何测试是否安装了字体?
【发布时间】:2018-10-29 07:50:14
【问题描述】:

我无法确定是否在一组机器上安装了自定义字体(欧几里德三角形)。

我使用了此处列出的代码 “Test if a Font is installed”,它适用于我的 Windows 10 机器。但它不适用于 Windows 7 机器和我客户的一堆机器。

所有机器都有 .Net 4.5 及更高版本。

如果我尝试列出机器上的所有字体,则字体未列出:

    static void ListFonts()
    {
        try
        {
            using (InstalledFontCollection fontsCollection = new InstalledFontCollection())
            {
                FontFamily[] fontFamilies = fontsCollection.Families;
                var fonts = new List<string>();
                foreach (FontFamily font in fontFamilies)
                    fonts.Add(font.Name);
                var file = new FileInfo(Assembly.GetExecutingAssembly().Path() + "\\fonts.txt");
                Serializer.SerializeToFile(fonts, file.FullName);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Printer Configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);
            var file = new FileInfo(Assembly.GetExecutingAssembly().Path() + "\\log.txt");
            File.WriteAllText(file.FullName, ex.ToString());
            Console.WriteLine(ex.ToString());
        }
    }

编辑:我已以管理员身份运行我的代码,以确认问题与权限无关。

【问题讨论】:

  • 只是猜测:其他机器权限不足?
  • 您的代码不会搜索特定字体,它只会将它们添加到列表中,然后将其写入文件……
  • 我知道我列出的代码不会搜索特定字体。我链接到的帖子包含大量示例 - 我已经尝试了所有这些,但在目标机器上都没有。
  • 因此,如果我正确阅读了您编辑的帖子,您说字体已安装(即可以从写字板使用)但在您使用代码列出字体时不显示?
  • 正确。该字体出现在 MS Word 等中并且可以使用。通过代码列出字体时找不到字体。

标签: c# .net windows fonts


【解决方案1】:

使用 LINQ,迭代已安装字体的列表并检查它是否包含特定字体本质上应该是单行(加上不可避免的样板):

static bool IsFontInstalled(string fontname)
{
    using (var ifc = new InstalledFontCollection())
    {
        return ifc.Families.Any(f => f.Name == fontname);
    }
}

【讨论】:

  • 我链接到的帖子包含您的示例 - 我已经尝试过了,但它在目标机器上不起作用。
猜你喜欢
  • 2010-09-11
  • 2010-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-22
  • 2010-11-11
  • 2016-12-18
  • 1970-01-01
相关资源
最近更新 更多