C#根据字体名通过注册表获取该字体文件路径(win10)两种方法推荐第二种
方法一:
直接先上源码:
private System.Collections.Generic.SortedDictionary<string, string> ReadFontInformation()
{
var dictionary = new System.Collections.Generic.SortedDictionary<string, string>(); Microsoft.Win32.RegistryKey localMachineKey = Microsoft.Win32.Registry.LocalMachine;
// 打开注册表
Microsoft.Win32.RegistryKey localMachineKeySub = localMachineKey.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", false); //获取字体名
string[] mynames = localMachineKeySub.GetValueNames(); foreach (string name in mynames)
{
//获取字体的文件名
string myvalue = localMachineKeySub.GetValue(name).ToString(); if (myvalue.Substring(myvalue.Length - 4).ToUpper() == ".TTF" && myvalue.Substring(1, 2).ToUpper() != @":\")
{
string val = name.Substring(0, name.Length - 11);
dictionary[val] = myvalue;
}
}
localMachineKeySub.Close();
return dictionary;
}
解决思路:
1. 打开windows/fonts目录, 右键下图, 勾选字体文件名称, 发现其实字体文件名称和显示的名称是有区别的


2. 然后去注册表中看看, win+r→regedit, 定位 LocalMachine \\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts 可以看到, 对应的键值对

3. 既然有思路了, 就开始操作注册表, 注意, 如果在win7以上的系统中, 将localMachineKey.OpenSubKey("",true)第二个参数设置为true, 在xp下没有问题, 在win7以上就会报以下错误:

4. 解决此错误的方法是增加应用程序配置清单文件, 然后做下面的修改, 但是在运行的时候, 会提示要求用管理员权限(提权). 全部代码和清单文件内容如下:
//[System.Security.Permissions.RegistryPermissionAttribute(System.Security.Permissions.SecurityAction.PermitOnly, Read = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts")]// 约束代码仅可读注册表
private System.Collections.Generic.SortedDictionary<string, string> ReadFontInformation()
{
var dictionary = new System.Collections.Generic.SortedDictionary<string, string>(); Microsoft.Win32.RegistryKey localMachineKey = Microsoft.Win32.Registry.LocalMachine;
// 打开注册表
Microsoft.Win32.RegistryKey localMachineKeySub = localMachineKey.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", false); //获取字体名
string[] mynames = localMachineKeySub.GetValueNames(); foreach (string name in mynames)
{
//获取字体的文件名
string myvalue = localMachineKeySub.GetValue(name).ToString(); if (myvalue.Substring(myvalue.Length - 4).ToUpper() == ".TTF" && myvalue.Substring(1, 2).ToUpper() != @":\")
{
string val = name.Substring(0, name.Length - 11);
dictionary[val] = myvalue;
}
}
localMachineKeySub.Close();
return dictionary;
}
清单文件:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC 清单选项
如果想要更改 Windows 用户帐户控制级别,请使用
以下节点之一替换 requestedExecutionLevel 节点。n
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" /> 指定 requestedExecutionLevel 元素将禁用文件和注册表虚拟化。
如果你的应用程序需要此虚拟化来实现向后兼容性,则删除此
元素。
-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo> <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- 设计此应用程序与其一起工作且已针对此应用程序进行测试的
Windows 版本的列表。取消评论适当的元素,Windows 将
自动选择最兼容的环境。 --> <!-- Windows Vista -->
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />--> <!-- Windows 7 -->
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />--> <!-- Windows 8 -->
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />--> <!-- Windows 8.1 -->
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />--> <!-- Windows 10 -->
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />--> </application>
</compatibility> <!-- 指示该应用程序可以感知 DPI 且 Windows 在 DPI 较高时将不会对其进行
自动缩放。Windows Presentation Foundation (WPF)应用程序自动感知 DPI,无需
选择加入。选择加入此设置的 Windows 窗体应用程序(目标设定为 .NET Framework 4.6 )还应
在其 app.config 中将 "EnableWindowsFormsHighDpiAutoResizing" 设置设置为 "true"。-->
<!--
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
</windowsSettings>
</application>
--> <!-- 启用 Windows 公共控件和对话框的主题(Windows XP 和更高版本) -->
<!--
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
--> </assembly>
方法二:直接拿对应的文件名:
public class FontNameFile
{
public static string getFontFileName(string fontname)
{
string folderFullName = System.Environment.GetEnvironmentVariable("windir") + "\\fonts";
DirectoryInfo TheFolder = new DirectoryInfo(folderFullName);
foreach (FileInfo NextFile in TheFolder.GetFiles())
{
if (NextFile.Exists)
{
if (fontname==getFontName(NextFile.FullName))
{ return NextFile.Name;
}
}
}
return "";
} private static string getFontName(string fontfilename)
{
PrivateFontCollection pfc = new PrivateFontCollection();
//只要ttf和TTF, 其它的本项目不需要
if (fontfilename.EndsWith(".ttf") || fontfilename.EndsWith(".TTF"))
{
pfc.AddFontFile(fontfilename);
}
if (pfc.Families.Length > 0)
{
return pfc.Families[0].Name;
}
return "";
}
}

C#根据字体名通过注册表获取该字体文件路径(win10)两种方法推荐第二种的更多相关文章
- 利用C#访问注册表获取软件的安装路径
文章地址:https://blog.csdn.net/yl2isoft/article/details/17332139
- C# 系统应用之通过注册表获取USB使用记录(一)
该文章是“个人电脑历史记录清除软件”项目的系统应用系列文章.前面已经讲述了如何清除IE浏览器的历史记录.获取Windows最近访问文件记录.清除回收站等功能.现在我需要完成的是删除USB设备上的U盘. ...
- OpenFileDialog获取文件名和文件路径问题
OpenFileDialog获取文件名和文件路径问题(转) 转自:http://blog.sina.com.cn/s/blog_7511914e0101cbjn.html System.IO.Path ...
- 四种方法获取可执行程序的文件路径(.NET Core / .NET Framework)
原文:四种方法获取可执行程序的文件路径(.NET Core / .NET Framework) 本文介绍四种不同的获取可执行程序文件路径的方法.适用于 .NET Core 以及 .NET Framew ...
- spring java 获取webapp下文件路径
spring java 获取webapp下文件路径 @RequestMapping("/act/worldcup_schedule_time/imgdownload") @Resp ...
- 『练手』通过注册表 获取 VS 和 SQLServer 文件路径
获取任意 VS 和 SQLServer 的 磁盘安装目录. 背景需求:如果磁盘电脑安装了 VS 或者 SQLServer 则 认定这台计算机 的使用者 是一名 软件研发人员,则让程序 以最高权限运行. ...
- C# 根据注册表获取当前用户的常用目录整理
1.使用C#获取当前程序或解决方案的路径 2.使用C#获取当前登录用户的相关目录 3.也可以获取当前系统通用目录 4.获取Windows系统的目录,从注册表中获取. 一.当前用户的目录,HKEY_Cu ...
- 读取注册表获取Windows系统XP/7/8/10类型(使用wcscmp比较wchar[]内容)
很多方案是采用GetVersion.GetVersionEx这两个API来查询操作系统的版本号来判断当前的操作系统是Windows系列中的哪个,在Win10没有出现前,这种方法是行的通的,但是Win1 ...
- Inno Setup中做补丁通过注册表获取原程序安装目录
今天找VM补丁看到的,是个innosetup封装的,所以习惯性的喜欢去看人家的iss文件是怎么编写的. DefaultDirName={reg:HKLM\SOFTWARE\VMware%2c%20In ...
随机推荐
- 2015 Syrian Private Universities Collegiate Programming Contest
A. Window B. Paper Game Des:给你一个矩形集合,一开始只有一个W*H的矩形.每次可以选一个矩形,切成两个并加入集合,长和宽必须是正整数.不能操作者输,求先手赢还是输.(1 ≤ ...
- spring: spittr实例 构建简单的web应用 Test测试用例
本例为Test,测试上一贴的程序 package spittr.web; import org.junit.Test; import org.springframework.test.web.serv ...
- 一个较好的style与ControlTemplate结合的示例(以Button为例)
<!--按钮背景画刷--> <LinearGradientBrush x:Key="buttonBackgroundBrush"> &l ...
- win8里DNW的裸机程序下载
1. win8要装DNW驱动首先要禁止驱动数字签名(参考百度经验:http://jingyan.baidu.com/article/3f16e003d1f4612591c103ce.html) 2.然 ...
- LeetCode OJ:Lowest Common Ancestor of a Binary Tree(最近公共祖先)
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- Java中会有内存泄露吗?
一.Java内存回收机制 不论哪种语言的内存分配方式,都需要返回所分配内存的真实地址,也就是返回一个指针到内存块的首地址.Java中对象是采用new或者反射的方法创建的, 这些对象的创建都是在堆(He ...
- OC-文件操作
一.归档NSKeyedArchiver========================== 1.第一种方式:存储一种数据.===================== //归档 //第一种写法 //对象 ...
- Mac os x 下配置Intellij IDEA + Tomcat 出现权限问题的解决办法
出现的错误提示如下: 下午9:11:27 All files are up-to-date下午9:11:27 All files are up-to-date下午9:11:27 Error runni ...
- HAWQ取代传统数仓实践(九)——维度表技术之退化维度
退化维度技术减少维度的数量,简化维度数据仓库模式.简单的模式比复杂的更容易理解,也有更好的查询性能. 有时,维度表中除了业务主键外没有其它内容.例如,在本销售订单示例中,订单维度表除了订 ...
- F. Coprime Subsequences
题目链接: F. Coprime Subsequences time limit per test 2 seconds memory limit per test 256 megabytes inpu ...