C # Gets the font file path (WIN10) from the registry based on the font name

Source: Internet
Author: User

Method One:

Direct first on the source:

        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; Open the registry Microsoft.Win32.RegistryKey localmachinekeysub = Localmachinekey.opensubkey ("Software\\microsoft                \\Windows nt\\currentversion\\fonts ", false);                Get font name string[] mynames = Localmachinekeysub.getvaluenames (); foreach (string name in Mynames) {//Gets the file name of the font string myvalue = Loca Lmachinekeysub.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; }

Solution Ideas:

1. Open the Windows/fonts directory, right-click, check the font file name, found that the font file name and display of the name is different

2. Then go to the registry to see, Win+r→regedit, locate LocalMachine \ \software\\microsoft\\windows nt\\currentversion\\fonts can see, Corresponding key-value pairs

3. Now that you have ideas, start operating the registry, note that if the Localmachinekey.opensubkey ("", true) second parameter is set to true in the system above Win7, there is no problem under XP, the following error will be reported above Win7:

4. The workaround for this error is to add the application configuration manifest file, and then make the following modifications, but at run time, you will be prompted to require Administrator privileges (right). The contents of all code and manifest files are as follows:

[System.Security.Permissions.RegistryPermissionAttribute ( System.Security.Permissions.SecurityAction.PermitOnly, Read = @ "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Nt\ Currentversion\fonts ")]//constraint code only readable registry private system.collections.generic.sorteddictionary<string, string> read Fontinformation () {var dictionary = new system.collections.generic.sorteddictionary<string, STRING&G                           t; ();                Microsoft.Win32.RegistryKey Localmachinekey = Microsoft.Win32.Registry.LocalMachine; Open the registry Microsoft.Win32.RegistryKey localmachinekeysub = Localmachinekey.opensubkey ("Software\\microsoft                \\Windows nt\\currentversion\\fonts ", false);                Get font name string[] mynames = Localmachinekeysub.getvaluenames (); foreach (string name in Mynames) {//Gets the file name of the font string myvalue = Loca Lmachinekeysub.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; }

Manifest file:

<?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.v 3 "> <!--UAC manifest option if you want to change the Windows user Account Control level, replace the requestedExecutionLevel node with one of the following nodes. n <requestedexecutionlevel level= "AsInvoker" uiaccess= "false"/> <requestedexecutionlevel level= "  Requireadministrator "uiaccess=" false "/> <requestedexecutionlevel level=" highestavailable "uiAccess=" false "            /> Specifies that the requestedExecutionLevel element disables file and registry virtualization.        If your application requires this virtualization for backward compatibility, delete this element. --<requestedexecutionlevel level= "Requireadministrator" uiaccess= "false"/> </requestedprivilege S> </security> </trustInfo> <Compatibility xmlns= "URN:SCHEMAS-MICROSOFT-COM:COMPATIBILITY.V1" > <application> <!--Designing this application to work with and for This application tests the list of Windows versions. Uncomment the appropriate elements and Windows will automatically select the most compatible environment. -<!--Windows Vista-<!--<supportedos id= "{e2011457-1546-43c5-a5fe-008deee3d3f0}"/&GT;--&G      T <!--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--&      Gt <!--<supportedos id= "{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>--> </application> </ Compatibility> <!--indicates that the application can be DPI-aware and that Windows will not automatically scale when the DPI is high. Windows Presentation Foundation (WPF) applications automatically sense DPI without the option to opt-in. Select the Windows forms application that is joined to this setting (the target is set to. NET FramewoRK 4.6) You should also set the "enablewindowsformshighdpiautoresizing" setting to "true" in its app. config.  -<!--<application xmlns= "Urn:schemas-microsoft-com:asm.v3" > <windowsSettings> <dpiaware  xmlns= "Http://schemas.microsoft.com/SMI/2005/WindowsSettings" >true</dpiAware> </windowsSettings> </application> <!--themes for Windows common Controls and dialogs enabled (Windows XP and later)--<!--<dependency> <          Dependentassembly> <assemblyidentity type= "Win32" Name= "Microsoft.windows.common-controls" version= "6.0.0.0" processorarchitecture= "*" publickeytoken= "6595B64144CCF1DF" language= " * "/> </dependentAssembly> </dependency>--></assembly>

  Method Two: Directly take the corresponding file name:

public class Fontnamefile {public static string Getfontfilename (String fontname) {string fo            Lderfullname = 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 Privat            Efontcollection (); As long as TTF and TTF, the other project does not require the IF (fontfilename. EndsWith (". Ttf") | | Fontfilename. EndsWith (". TTF ")) {Pfc.            Addfontfile (Fontfilename); } if (Pfc. Families.length > 0) {return pfc. Families[0].            Name;        } return ""; }    }

C # Gets the font file path (WIN10) from the registry based on the font name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.