我使用此代码并且工作正常
Imports System.Drawing.Text
Dim fontInstalled As Boolean = False
Dim fontToSearch As String
Dim fonts As New InstalledFontCollection
fontToSearch = "Verdana"
For Each one As FontFamily In fonts.Families
'If I want to show every installed family name
'MsgBox(one.Name)
If one.Name = fontToSearch Then
fontInstalled = True
MsgBox("Font " & fontToSearch & " IS installed!!!")
Exit For
End If
Next
If fontInstalled = False Then MsgBox("Font " & fontToSearch & " is NOT installed")
但我确信使用InstalledFontCollection会有更清晰的解决方案,但我无法将此代码改编为VB.NET Test if a Font is installed
var fontsCollection = new InstalledFontCollection();
foreach (var fontFamiliy in fontsCollection.Families)
{
if (fontFamiliy.Name == fontName) ... \\ installed
}
答案 0 :(得分:2)
每当您使用关键字New
时,请检查它是否包含Dispose
方法。如果是,则意味着它可能会分配一些需要Disposed
的资源。因此,请在Using
块
<强> 1。检查是否已安装
Dim IsInstalled As Boolean
Using fnts As New InstalledFontCollection()
IsInstalled = fnts.Families.
AsEnumerable().
Select(Function(s) s.Name).
Contains("Verdana")
End Using
<强> 2。试一试
Using fnt As New Font("Verdana", 12)
IsInstalled = (fnt IsNot Nothing)
End Using
第3。获取已安装名称列表
Dim fontNames As String()
Using fnts As New InstalledFontCollection()
fontNames = fnts.Families.
AsEnumerable().
Select(Function(s) s.Name).
ToArray()
End Using
奖金提示
MsgBox
/ MessageBox
是一种可怕的调试方式。从菜单中选择 Debug | Windows |输出即可。您可以将其停靠在任何您想要的地方,并将其设置为&#34;自动隐藏&#34;所以它隐藏,除非鼠标在它上面。要输出到它:
For Each s As String In fontNames
Console.WriteLine(s)
Next
et voila 不再点击处理172个字体名称对话框。
答案 1 :(得分:1)
这样的支票可能会有所帮助:
imports system.drawing.text
...
Function IsFontInstalled(FontName As String) As Boolean
Dim fonts As New InstalledFontCollection
For Each one As FontFamily In fonts.Families
If one.Name = FontName Then
Return True
End If
Next
Return False
End Function
熟悉这种技术:
Function IsFontInstalled(FontName As String) As Boolean
Using fnt As New Font(FontName, 12)
Return (fnt IsNot Nothing)
End Using
End Function
如此短暂,你可以使用该技术。