C#根据字体名通过注册表获取该字体文件路径(win10)

时间:2022-05-14 19:52:12

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目录, 右键下图, 勾选字体文件名称, 发现其实字体文件名称和显示的名称是有区别的

C#根据字体名通过注册表获取该字体文件路径(win10)

C#根据字体名通过注册表获取该字体文件路径(win10)

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

C#根据字体名通过注册表获取该字体文件路径(win10)

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

C#根据字体名通过注册表获取该字体文件路径(win10)

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; }

清单文件:

C#根据字体名通过注册表获取该字体文件路径(win10)

<?xml version="1.0" encoding="utf-8"?> <assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> <assemblyIdentity version="1.0.0.0"/> <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 />--> <!-- Windows 7 --> <!--<supportedOS />--> <!-- Windows 8 --> <!--<supportedOS />--> <!-- Windows 8.1 --> <!--<supportedOS />--> <!-- Windows 10 --> <!--<supportedOS />--> </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" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" /> </dependentAssembly> </dependency> --> </assembly>

方法二:直接拿对应的文件名: