Skip to content

Commit 487fbcf

Browse files
authored
Install fonts through powershell (#7394)
1 parent 5377d35 commit 487fbcf

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/Files.Launcher/MessageHandlers/ContextMenuHandler.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Newtonsoft.Json;
44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
67
using System.IO.Pipes;
78
using System.Linq;
89
using System.Threading.Tasks;
@@ -95,14 +96,31 @@ private object HandleMenuMessage(Dictionary<string, object> message, DisposableD
9596
var cMenuExec = table.GetValue<ContextMenu>("MENU");
9697
if (message.TryGetValue("ItemID", out var menuId))
9798
{
98-
switch (message.Get("CommandString", (string)null))
99+
var isFont = new[] { ".fon", ".otf", ".ttc", ".ttf" }.Contains(Path.GetExtension(cMenuExec.ItemsPath[0]), StringComparer.OrdinalIgnoreCase);
100+
var verb = message.Get("CommandString", (string)null);
101+
switch (verb)
99102
{
100-
case "mount":
103+
case string _ when verb == "install" && isFont:
104+
{
105+
var userFontDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "Fonts");
106+
var destName = Path.Combine(userFontDir, Path.GetFileName(cMenuExec.ItemsPath[0]));
107+
Win32API.RunPowershellCommand($"-command \"Copy-Item '{cMenuExec.ItemsPath[0]}' '{userFontDir}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(cMenuExec.ItemsPath[0])}' -Path 'HKCU:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts' -PropertyType string -Value '{destName}'\"", false);
108+
}
109+
break;
110+
111+
case string _ when verb == "installAllUsers" && isFont:
112+
{
113+
var winFontDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Fonts");
114+
Win32API.RunPowershellCommand($"-command \"Copy-Item '{cMenuExec.ItemsPath[0]}' '{winFontDir}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(cMenuExec.ItemsPath[0])}' -Path 'HKLM:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts' -PropertyType string -Value '{Path.GetFileName(cMenuExec.ItemsPath[0])}'\"", true);
115+
}
116+
break;
117+
118+
case string _ when verb == "mount":
101119
var vhdPath = cMenuExec.ItemsPath[0];
102120
Win32API.MountVhdDisk(vhdPath);
103121
break;
104122

105-
case "format":
123+
case string _ when verb == "format":
106124
var drivePath = cMenuExec.ItemsPath[0];
107125
Win32API.OpenFormatDriveDialog(drivePath);
108126
break;

0 commit comments

Comments
 (0)