Files
font-preview/FontPreview/InstallRunner.cs
T

54 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace FontPreview;
// 被提权(runas)拉起的无界面安装子进程:
// 主程序以普通权限运行,复制好字体文件后,用 runas 启动自身并带 /install "<目录>" 参数,
// 本子进程只负责把该目录里的字体注册为系统字体,完成后弹个结果框即退出,主程序不重启、不受影响。
internal static class InstallRunner
{
internal static void Run(string dir)
{
if (string.IsNullOrEmpty(dir) || !Directory.Exists(dir))
{
MessageBox.Show("安装目录不存在:" + dir, AppInfo.AppName + " 安装", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
HashSet<string> exts = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".ttf", ".otf", ".ttc", ".ote" };
int added = 0, skipped = 0, failed = 0;
List<string> failNames = new List<string>();
try
{
foreach (string f in Directory.GetFiles(dir))
{
if (!exts.Contains(Path.GetExtension(f))) continue;
string lnk, reg;
if (!FontManagerEx.InstallAsShortcut(f, out lnk, out reg))
{
failed++;
failNames.Add(Path.GetFileName(f));
continue;
}
if (lnk != null && lnk.StartsWith("(已安装")) skipped++;
else added++;
}
}
catch (Exception ex)
{
MessageBox.Show("安装过程异常:" + ex.Message, AppInfo.AppName + " 安装", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string msg = "系统字体安装完成:\r\n新注册 " + added + " 个,已存在跳过 " + skipped + " 个,失败 " + failed + " 个。";
if (failed > 0)
{
msg += "\r\n\r\n失败项:";
foreach (string n in failNames) msg += "\r\n " + n;
}
msg += "\r\n\r\n若在 Word/PS 等软件中未立即出现,请注销重新登录或重启后查看。";
MessageBox.Show(msg, AppInfo.AppName + " 安装", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}