4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

フォントをインストールする

Posted at

仕事で自作のセットアッププログラムの中から Windows にフォントをインストールする必要が出てきた。
最近はフォントフォルダにコピーするだけではダメで、レジストリに登録する必要があるみたい。とりあえず見よう見まねでレジストリに登録するようコードを書いてみたけど何故だか上手くいかず、結局、Shell.Application をOLEオートメーション(って今でも言うの?)で操作する方法で何とか解決できた。

以下はそのサンプル。

sample.cs

var font_path = @"C:\PATH\TO\FONT.TTF";
if (File.Exists(font_path))
{
  Type instanceType = Type.GetTypeFromProgID("Shell.Application");
  dynamic shellApplicaition = Activator.CreateInstance(instanceType);
  dynamic folder = shellApplicaition.Namespace(Path.GetDirectoryName(font_path));
  dynamic folderItem = folder.ParseName(Path.GetFileName(font_path));
  folderItem.InvokeVerb("Install");
}
else
  MessageBox.Show("File not exist");
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?