如何以编程方式安装字体 (C#) [英] How to install a Font programmatically (C#)

查看:44
本文介绍了如何以编程方式安装字体 (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法以编程方式将字体永久添加到 Windows 7/8 PC?我已经阅读了几篇关于 AddFontResource DLL-Import 的帖子,但它似乎不起作用.

is there a way to permanently add a Font to a Windows 7/8 PC programmatically? I have read several posts about the AddFontResource DLL-Import, but it doesn't seem to work.

除此之外,MSDN 文档 表示重新启动计算机后字体将被删除,除非将该字体添加到注册表中.

Besides of that, the MSDN Documentation says the font will be deleted after a restart of the computer, unless the font is added into the registry.

如何永久安装字体?如何将字体添加到注册表?它总是相同的名称/条目吗?

How can I install a font permanently? How can I add the font to the registry? Is it always the same name/entry?

我必须在运行时动态添加字体,因为我会在用户选择字体后立即获取该字体.

I have to add the font dynamically on runtime, because I get the font as soon as the user selects it.

备注:我知道如何添加注册表项.我的问题更多是关于 Windows XP、Vista、7 和 8 以及不同字体类型之间的兼容性.也许有一种方法可以启动另一个为我安装字体的 exe.

Remark: I know how to add a registry entry. My question is more about the compatibility between Windows XP, Vista, 7 and 8 and the different font-types. Maybe there is a way to start an other exe which installs the font for me.

推荐答案

正如您提到的,您可以启动其他可执行文件来为您安装 TrueType 字体.我不知道您的具体用例,但我会整理出我所知道的方法,也许其中一种对您有用.

As you mentioned, you can launch other executables to install TrueType Fonts for you. I don't know your specific use cases but I'll run down the methods I know of and maybe one will be of use to you.

Windows 有一个名为 fontview.exe 的内置实用程序,您只需调用 Process.Start("Path ofile.ttf") 即可调用它在任何有效的 TrueType 字体上...假设默认文件关联.这类似于从 Windows 资源管理器手动启动它.这里的优点是它真的很简单,但它仍然需要每个字体的用户交互才能安装.据我所知,没有办法调用这个过程的安装"部分作为参数,但即使有,你仍然需要提升权限并与 UAC 作斗争.

Windows has a built-in utility called fontview.exe, which you can invoke simply by calling Process.Start("Path ofile.ttf") on any valid TrueType Font... assuming default file associations. This is akin to launching it manually from Windows Explorer. The advantage here is it's really trivial, but it still requires user interaction per font to install. As far as I know there is no way to invoke the "Install" portion of this process as an argument, but even if there was you'd still have to elevate permissions and battle UAC.

更有趣的选项是一个名为 FontReg 的实用程序,它取代了已弃用的 fontinst.exe包含在旧版本的 Windows 中.FontReg 使您能够通过使用 /copy 开关调用可执行文件,以编程方式安装整个 Fonts 目录:

The more intriguing option is a utility called FontReg that replaces the deprecated fontinst.exe that was included on older versions of Windows. FontReg enables you to programatically install an entire directory of Fonts by invoking the executable with the /copy switch:

    var info = new ProcessStartInfo()
        {
            FileName = "Path	oFontReg.exe",
            Arguments = "/copy",
            UseShellExecute = false,
            WindowStyle = ProcessWindowStyle.Hidden

        };

   Process.Start(info);

请注意,字体必须位于 FontReg.exe 所在位置的根目录中.您还必须拥有管理员权限.如果您需要完全透明的字体安装,我建议您使用提升的权限启动您的应用程序并预​​先批准 UAC,这样当您生成子进程时,您就不需要用户批准 权限内容

Note that the Fonts have to be in the root of wherever FontReg.exe is located. You'll also have to have administrator privileges. If you need your Font installations to be completely transparent, I would suggest launching your application with elevated permissions and approve of the UAC up front, that way when you spawn your child processes you wont need user approval Permissions stuff

这篇关于如何以编程方式安装字体 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆