如何C#程序安裝windows系統(tǒng)字體?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)專注于喀什企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,商城網(wǎng)站建設(shè)??κ簿W(wǎng)站建設(shè)公司,為喀什等地區(qū)提供建站服務(wù)。全流程按需定制,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)1.1、使用代碼安裝字體
注意:安裝字體時(shí),需要windows的管理員權(quán)限。
[DllImport("kernel32.dll", SetLastError = true)] public static extern int WriteProfileString(string lpszSection, string lpszKeyName, string lpszString); [DllImport("gdi32")] public static extern int AddFontResource(string lpFileName); /// <summary> /// 安裝字體 /// </summary> /// <param name="fontFilePath">字體文件全路徑</param> /// <returns>是否成功安裝字體</returns> /// <exception cref="UnauthorizedAccessException">不是管理員運(yùn)行程序</exception> /// <exception cref="Exception">字體安裝失敗</exception> public static bool InstallFont(string fontFilePath) { try { System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); //判斷當(dāng)前登錄用戶是否為管理員 if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator) == false) { throw new UnauthorizedAccessException("當(dāng)前用戶無管理員權(quán)限,無法安裝字體。"); } //獲取Windows字體文件夾路徑 string fontPath=Path.Combine(System.Environment.GetEnvironmentVariable("WINDIR") , "fonts",Path.GetFileName(fontFilePath)); //檢測(cè)系統(tǒng)是否已安裝該字體 if (!File.Exists(fontPath)) { // File.Copy(System.Windows.Forms.Application.StartupPath + "\\font\\" + FontFileName, FontPath); //font是程序目錄下放字體的文件夾 //將某路徑下的字體拷貝到系統(tǒng)字體文件夾下 File.Copy(fontFilePath, fontPath); //font是程序目錄下放字體的文件夾 AddFontResource(fontPath); //Res = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0); //WIN7下編譯會(huì)出錯(cuò),不清楚什么問題。注釋就行了。 //安裝字體 WriteProfileString("fonts", Path.GetFileNameWithoutExtension(fontFilePath) + "(TrueType)", Path.GetFileName(fontFilePath)); } } catch (Exception ex) { throw new Exception(string.Format($"[{Path.GetFileNameWithoutExtension(fontFilePath)}] 字體安裝失??!原因:{ex.Message}" )); } return true; }
新聞標(biāo)題:如何C#程序安裝windows系統(tǒng)字體-創(chuàng)新互聯(lián)
文章出自:http://muchs.cn/article16/dhiigg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、建站公司、自適應(yīng)網(wǎng)站、網(wǎng)站排名、搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容