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 ...
随机推荐
- 使用springmvc时报错JSPs only permit GET POST or HEAD
两个地方需要注意:第一处在web.xml文件中不要忘记配置 <filter> <filter-name>HiddenHttpMethodFilter</filter-na ...
- 利用JS获取地址栏的中文参数
地址栏中为:localhost:22865/ZYHSYY.aspx?BQH=305&DoctorName=张三&DoctorId=100我想利用JS获取到“张三”,请问该如何写js?目 ...
- angular一些常用的方法:
angular.copy(); 用法:对Object对象的深度拷贝$scope.data = {name:'yanjinyun',age:'11'}; $scope.origData = angula ...
- shell read 命令 (转)
read命令 -p(提示语句) -n(字符个数) -t(等待时间) -s(不回显) 1.基本读取read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说).得到输入后,read命令将数 ...
- javascrip函数简单介绍
JavaScript 函数定义JavaScript 使用关键字 function 定义函数.函数可以通过声明定义,也可以是一个表达式.函数声明在之前的教程中,你已经了解了函数声明的语法 :functi ...
- hdu4715
题解: 二分图判断 建立原图的补图 判断是否是二分图 代码: #include<cstdio> #include<cmath> #include<cstring> ...
- 记录下MD5加密遇到的坑
错误的写法: public static String md5(String plainText) { byte[] secretBytes = null; try { secretBytes = M ...
- 前端 velocity(.vm)模板里写ajax出现解析异常
异常信息:Caused by: org.apache.velocity.exception.ParseErrorException: Encountered "{" at dist ...
- PHP生成UTF-8编码的CSV文件用Excel打开乱码的问题
在你要输出的内容前先输出"\xEF\xBB\xBF",例如:你要输出的内容保存在$content里$content = "\xEF\xBB\xBF".$cont ...
- android 进制转换方法
import android.util.Log; public class CommandHelper { public static String intToHexString(int value) ...