本帖最后由 小白龙 于 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/
另外执行完后, 双击打开字体, 仍然可以点安装按钮安装字体, 看来代码没发挥作用,没有安装上, 如果安装上了, 当我再次点击 安装 按钮, 会提示该字体安装过
求路过大佬帮助, 谢谢- # 定义字体文件路径
- $fontFilePath = ".\A.ttf"
- #获取字体名
- Add-Type -AssemblyName System.Drawing
- $fontCollection = New-Object System.Drawing.Text.PrivateFontCollection
- $fontCollection.AddFontFile($fontFilePath)
- $fontName = $fontCollection.Families[0].Name
- $fontName
- # 检查系统中是否已安装指定字体
- $fontInstalled = $false
- $installedFontCollection = New-Object System.Drawing.Text.InstalledFontCollection
- $installedFontFamilies = $installedFontCollection.Families
- foreach ($fontFamily in $installedFontFamilies)
- {
- if ($fontFamily.Name -eq $fontName)
- {
- $fontInstalled = $true
- break
- }
- }
- $fontInstalled
- # 如果未安装该字体,则添加到系统字体表中
- if (!$fontInstalled)
- {
- # 复制字体文件到系统 Font 目录
- $destinationFolderPath = "C:\Windows\Fonts"
- Copy-Item -Path $fontFilePath -Destination $destinationFolderPath -Force
-
- # 将字体文件注册到系统字体表中
- Add-Type -TypeDefinition @'
- using System;
- using System.Runtime.InteropServices;
- public static class FontInstaller
- {
- [DllImport("gdi32.dll")]
- private static extern int AddFontResource(string lpFileName);
- public static int Install(string fontFilePath)
- {
- return AddFontResource(fontFilePath);
- }
- }
- '@
- [FontInstaller]::Install($fontFilePath)
-
- #删除字体
- del $fontFilePath -Force -Recurse
- }
复制代码 |