找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 13961|回复: 4

[问题求助] powershell安装注册字体

[复制链接]
发表于 2023-5-29 12:42:35 | 显示全部楼层 |阅读模式
本帖最后由 小白龙 于 2023-5-29 17:24 编辑

我想用批处理安装免费可商用的 鸿蒙字体 https://www.hanyi.com.cn/custom-font-case-7

然后,我用GPT写了一段代码, 代码的任务是先获取字体的文件的字体名, 然后检查该字体是否已经安装, 如是要没有安装, 则将字体拷贝到windows下的Font文件夹, 然后安装Font文件夹下的该字体, 然后删除原字体文件
执行中报错  无法删除字体  但是我手动是可以删除的! 我去windows下的Font文件夹看来了一下,拷贝到Font目录的命令也没生效!
就算没有权限, 我用下面的小工具提权也无法删除, 也没拷贝成功, 真是怪了, PS:把批处理拖到这个软件的图标上就能提权执行
https://www.sordum.org/9416/powe ... highest-privileges/

另外执行完后, 双击打开字体, 仍然可以点安装按钮安装字体, 看来代码没发挥作用,没有安装上, 如果安装上了, 当我再次点击 安装 按钮, 会提示该字体安装过

求路过大佬帮助, 谢谢
  1. # 定义字体文件路径
  2. $fontFilePath = ".\A.ttf"

  3. #获取字体名
  4. Add-Type -AssemblyName System.Drawing
  5. $fontCollection = New-Object System.Drawing.Text.PrivateFontCollection
  6. $fontCollection.AddFontFile($fontFilePath)
  7. $fontName = $fontCollection.Families[0].Name
  8. $fontName

  9. # 检查系统中是否已安装指定字体
  10. $fontInstalled = $false
  11. $installedFontCollection = New-Object System.Drawing.Text.InstalledFontCollection
  12. $installedFontFamilies = $installedFontCollection.Families
  13. foreach ($fontFamily in $installedFontFamilies)
  14. {
  15.         if ($fontFamily.Name -eq $fontName)
  16.         {
  17.                 $fontInstalled = $true
  18.                 break
  19.         }
  20. }
  21. $fontInstalled

  22. # 如果未安装该字体,则添加到系统字体表中
  23. if (!$fontInstalled)
  24. {
  25.         # 复制字体文件到系统 Font 目录
  26.         $destinationFolderPath = "C:\Windows\Fonts"
  27.         Copy-Item -Path $fontFilePath -Destination $destinationFolderPath -Force
  28.        
  29.         # 将字体文件注册到系统字体表中
  30.         Add-Type -TypeDefinition @'
  31. using System;
  32. using System.Runtime.InteropServices;

  33. public static class FontInstaller
  34. {
  35.     [DllImport("gdi32.dll")]
  36.     private static extern int AddFontResource(string lpFileName);

  37.     public static int Install(string fontFilePath)
  38.     {
  39.         return AddFontResource(fontFilePath);
  40.     }
  41. }
  42. '@
  43.         [FontInstaller]::Install($fontFilePath)
  44.        
  45.         #删除字体
  46.         del $fontFilePath -Force -Recurse
  47. }
复制代码
发表于 2023-5-29 17:48:30 | 显示全部楼层
提权后是不是当前目录变了
 楼主| 发表于 2023-5-29 18:40:19 | 显示全部楼层
本帖最后由 小白龙 于 2023-5-29 18:56 编辑

回复 2# idwma


    没有, 我在CMD下, 用 dir *mn* 能看到拷贝的文件在Font文件夹里面了,
最后那条命令的意思是,既然已经拷贝到Font文件夹了, 那原字体文件就没什么用了, 就把它删除, 但是死活执行不成功, 字体也没有安装上, 在别的软件下找不到安装的字体, 真是怪了
  1. <# :
  2. cls&echo off&cd /d "%~dp0"&mode con lines=5000&rem bat保存为ANSI/GB2312编码
  3. set "current=%cd%"
  4. set "nm=%~n0.bat"
  5. powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal '%~f0'|Out-String|Invoke-Expression"
  6. pause
  7. exit
  8. #>

  9. # 定义字体文件路径
  10. $fontFilePath = "$env:current\A.ttf"
  11. $fontFilePath

  12. #获取字体名
  13. Add-Type -AssemblyName System.Drawing
  14. $fontCollection = New-Object System.Drawing.Text.PrivateFontCollection
  15. $fontCollection.AddFontFile($fontFilePath)
  16. $fontName = $fontCollection.Families[0].Name
  17. $fontName

  18. # 检查系统中是否已安装指定字体
  19. $fontInstalled = $false
  20. $installedFontCollection = New-Object System.Drawing.Text.InstalledFontCollection
  21. $installedFontFamilies = $installedFontCollection.Families
  22. foreach ($fontFamily in $installedFontFamilies)
  23. {
  24.         if ($fontFamily.Name -eq $fontName)
  25.         {
  26.                 $fontInstalled = $true
  27.                 break
  28.         }
  29. }
  30. $fontInstalled

  31. # 如果未安装该字体,则添加到系统字体表中
  32. if (!$fontInstalled)
  33. {
  34.         # 复制字体文件到系统 Font 目录
  35.         $destinationFolderPath = "C:\Windows\Fonts"
  36.         Copy-Item -Path $fontFilePath -Destination $destinationFolderPath -Force
  37.        
  38.         # 将字体文件注册到系统字体表中
  39.         Add-Type -TypeDefinition @'
  40. using System;
  41. using System.Runtime.InteropServices;

  42. public static class FontInstaller
  43. {
  44.     [DllImport("gdi32.dll")]
  45.     private static extern int AddFontResource(string lpFileName);

  46.     public static int Install(string fontFilePath)
  47.     {
  48.         return AddFontResource(fontFilePath);
  49.     }
  50. }
  51. '@
  52.         [FontInstaller]::Install("C:\Windows\Fonts\A.ttf")
  53.        
  54.         #删除字体
  55.         del $fontFilePath -Force
  56. }
复制代码
 楼主| 发表于 2023-5-29 21:26:51 | 显示全部楼层
本帖最后由 小白龙 于 2023-5-29 21:29 编辑

太奇怪了, 用批处理删不了字体文件, 手动一下就删了, 一直都是下面的提示, 用powerrun也不行

del : 无法删除项 C:\Users\Administrator\Desktop\A.ttf: 对路径“C:\Users\Administrator\Desktop\A.ttf”的访问被拒绝。
发表于 2023-6-13 16:58:40 | 显示全部楼层
批处理用管理员身份运行试试,对font文件夹操作,需要的权限是比较大的。我感觉,你的需求,用简单的copy就能实现,判断字体存不存在,都不用,如果已经有了,它自己会提示你已经有了,如果不想有提示,可以增加参数,强制复制,不显示交互
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 01:44 , Processed in 0.017930 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表