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

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

问题描述

是否可以通过编程将字体永久添加到Windows 7/8 PC?
我已经阅读了几篇有关AddFontResource DLL导入的文章,但是似乎没有用。

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\to\file.ttf)即可调用在任何有效的TrueType字体上...假定默认文件关联。这类似于从Windows资源管理器手动启动它。这样做的好处是它确实是微不足道的,但仍然需要用户与每种字体进行交互才能安装。据我所知,无法调用此过程的安装部分作为参数,但是即使有,您仍然必须提升权限并与UAC作战。

Windows has a built-in utility called fontview.exe, which you can invoke simply by calling Process.Start("Path\to\file.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的实用程序替换旧版本Windows中已弃用的fontinst.exe。 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\to\FontReg.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天全站免登陆