Installing Fonts programatically C #,
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. IO; using System. runtime. interopServices; using Microsoft. win32; using System. drawing. text; namespace itextcsharpDemo {// <summary> //// </summary> public partial class Form4: Form {int result =-1; int error = 0; [DllImport ("kernel32.dll", SetLastError = true)] static extern int WriteProfileString (string lpszSection, string lpszKeyName, string lpszString); [DllImport ("user32.dll")] public static extern int SendMessage (int hWnd, // handle to destination window uint Msg, // message int wParam, // first message parameter int lParam // second message parameter ); [DllImport ("gdi32")] public static extern I Nt AddFontResource (string lpFileName ); /// <summary> ///// </summary> /// <param name = "FontFileName"> </param> /// <param name = "FontName ""> </param> private void installFont (string FontFileName, string FontName) {string WinFontDir = "C: \ windows \ fonts"; // string FontFileName = "DS-Digital Bold Italic. TTF "; // string FontName =" DS-Digital Bold Italic "; int Ret; int Res; string FontPath; const int W M_FONTCHANGE = 0x001D; const int HWND_BROADCAST = 0 xffff; FontPath = WinFontDir + "\" + FontName; if (! File. exists (FontPath) {File. copy (FontFileName, FontPath); // System. windows. forms. application. startupPath + "\ DS-Digital Bold Italic. TTF "Ret = AddFontResource (FontPath); Res = SendMessage (HWND_BROADCAST, WM_FONTCHANGE, 0, 0); Ret = WriteProfileString (" fonts ", FontName +" (TrueType )", fontFileName) ;}} [DllImport ("gdi32", EntryPoint = "AddFontResource")] public static extern int AddFontResourceA (String lpFileName); // [System. runtime. interopServices. dllImport ("gdi32.dll")] // private static extern int AddFontResource (string lpszFilename); [System. runtime. interopServices. dllImport ("gdi32.dll")] private static extern int CreateScalableFontResource (uint fdwHidden, string cursor, string lpszFontFile, string lpszCurrentPath); // [DllImport ("gdi32.dll ", entryPoint = "RemoveFontResourceW", S EtLastError = true)] public static extern int RemoveFontResource ([In] [financialas (UnmanagedType. LPWStr)] string lpFileName ); /// <summary> // installfont on the user's system and adds it to the registry so it's available on the next session // Your font must be pinned DED in your project with its build path set to 'content' and its Copy property // set to 'Copy alway' // win 10 has no permission to Tu juwen note /// </ Summary> // <param name = "contentFontName"> Your font to be passed as a resource (I. e. "myfont. tff ") </param> private static void RegisterFont (string contentFontName) {try {// Creates the full path where your font will be installed var fontDestination = Path. combine (System. environment. getFolderPath (System. environment. specialFolder. fonts), contentFontName); if (! File. exists (fontDestination) {// Copies font to destination System. IO. file. copy (Path. combine (System. IO. directory. getCurrentDirectory (), contentFontName), fontDestination); // Retrieves font name // Makes sure you reference System. drawing PrivateFontCollection fontCol = new PrivateFontCollection (); fontCol. addFontFile (fontDestination); var actualFontName = fontCol. families [0]. name; // Add font AddF OntResource (fontDestination); // Add registry entry Registry. setValue (@ "HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ Fonts", actualFontName, contentFontName, RegistryValueKind. string) ;}} catch (Exception ex) {MessageBox. show (ex. message. toString ());}} /// <summary> ///// </summary> /// <param name = "NombreFnt"> </param> /// <param name = "RutaFnt "> </param> internal static void InstalarFuente (string NombreFnt, string RutaFnt) {string CMD = string. format ("copy/Y \" {0} \ "\" % WINDIR % \ Fonts \ "", RutaFnt); EjecutarCMD (CMD); System. IO. fileInfo FInfo = new System. IO. fileInfo (RutaFnt); CMD = string. format ("reg add \" HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ Fonts \ "/v \" {0} \ "/t REG_SZ/d {1}/f ", nombreFnt, FInfo. name); EjecutarCMD (CMD);} // <summ Ary> //// </summary> /// <param name = "Comando"> </param> public static void EjecutarCMD (string Comando) {System. diagnostics. processStartInfo Info = new System. diagnostics. processStartInfo ("cmd.exe"); Info. arguments = string. format ("/c {0}", Comando); Info. windowStyle = System. diagnostics. processWindowStyle. hidden; System. diagnostics. process. start (Info);} // <summary> //// </summary> public Form4 () {InitializeComponent ();} /// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> private void Form4_Load (object sender, eventArgs e) {}/// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void button#click (object sender, eventArgs e) {try {// Chinese. ttf // xinti Mung Bean. ttf string fontname = System. wind Ows. forms. application. startupPath + @ "\ font \. ttf "; //". ttf "; // win 10 successfully installed Senty Pea NIDI mung bean body // windows/syste32/shell32.dll // Shell32.Shell shell = new Shell32.Shell (); // Shell32.Folder fontFolder = shell. nameSpace (0x14); // fontFolder. copyHere (fontname); // No permission // RegisterFont (fontname); installFont (fontname, ". ttf "); // invalid // result = AddFontResource (fontname); // error = Marshal. getLa StWin32Error (); // if (error! = 0) // {// MessageBox. show (new Win32Exception (error ). message); //} catch (Exception ex) {MessageBox. show (ex. message. toString ());}} /// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> private void button2_Click (object sender, eventArgs e) {FontFamily [] fontList = new System. drawing. text. installedFontCollection (). families; InstalledFontCollection installedFonts = new InstalledFontCollection (); List <FontList> list = new List <FontList> (); int id = 0; foreach (FontFamily font in installedFonts. families) {FontList l = new FontList (); l. fontName = font. name; l. fontId = id; list. add (l); id ++;} listBox1.DataSource = list; listBox1.ValueMember = "FontId"; listBox1.DisplayMember = "FontName ";} /// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> private void listbox#selectedindexchanged (object sender, eventArgs e) {if (listBox1.Items. count> 0) {object o = this. listBox1.SelectedItem; FontList od = new FontList (); od = o as FontList; // MessageBox. show (od. fontName); this. textBox1.Text = od. fontName ;}}/// <summary >///// </summary> public class FontList {public int FontId {get; set;} public string FontName {get; set ;}}}
Using System; using System. collections. generic; using System. linq; using System. windows. forms; using System. diagnostics; using System. reflection; using System. security. principal; namespace itextcsharpDemo {static class Program {// <summary> // main entry point of the application. /// </Summary> [STAThread] static void Main () {// Use Permission var wi = WindowsIdentity. getCurrent (); var wp = new WindowsPrincipal (wi); bool runAsAdmin = wp. isInRole (WindowsBuiltInRole. administrator); // Administrator permission if (! RunAsAdmin) {// It is not possible to launch a ClickOnce app as administrator directly, // so instead we launch the app as administrator in a new process. var processInfo = new ProcessStartInfo (Assembly. getExecutingAssembly (). codeBase); // The following properties run the new process as administrator processInfo. useShellExecute = true; processInfo. verb = "runas"; // Start the new process try {Pro Cess. start (processInfo);} catch (Exception) {// The user did not allow the application to run as administrator MessageBox. show ("Sorry, but I don't seem to be able to start" + "this program with administrator rights! ");} // Shut down the current process Application. exit ();} else {Application. enableVisualStyles (); Application. setCompatibleTextRenderingDefault (false); Application. run (new ManageForm ());}}}}
Https://stackoverflow.com/questions/14796162/how-to-install-a-windows-font-using-c-sharp