From 3a603ff5f0c1ddd047d65318e49c44b6dd4eedc6 Mon Sep 17 00:00:00 2001 From: Marco Gavelli Date: Fri, 31 Dec 2021 17:39:31 +0100 Subject: [PATCH] Install fonts through powershell --- .../MessageHandlers/ContextMenuHandler.cs | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Files.Launcher/MessageHandlers/ContextMenuHandler.cs b/src/Files.Launcher/MessageHandlers/ContextMenuHandler.cs index c9b73aa36145..dc76cbcb9fc0 100644 --- a/src/Files.Launcher/MessageHandlers/ContextMenuHandler.cs +++ b/src/Files.Launcher/MessageHandlers/ContextMenuHandler.cs @@ -3,6 +3,7 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.IO; using System.IO.Pipes; using System.Linq; using System.Threading.Tasks; @@ -95,14 +96,31 @@ private object HandleMenuMessage(Dictionary message, DisposableD var cMenuExec = table.GetValue("MENU"); if (message.TryGetValue("ItemID", out var menuId)) { - switch (message.Get("CommandString", (string)null)) + var isFont = new[] { ".fon", ".otf", ".ttc", ".ttf" }.Contains(Path.GetExtension(cMenuExec.ItemsPath[0]), StringComparer.OrdinalIgnoreCase); + var verb = message.Get("CommandString", (string)null); + switch (verb) { - case "mount": + case string _ when verb == "install" && isFont: + { + var userFontDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "Fonts"); + var destName = Path.Combine(userFontDir, Path.GetFileName(cMenuExec.ItemsPath[0])); + 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); + } + break; + + case string _ when verb == "installAllUsers" && isFont: + { + var winFontDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Fonts"); + 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); + } + break; + + case string _ when verb == "mount": var vhdPath = cMenuExec.ItemsPath[0]; Win32API.MountVhdDisk(vhdPath); break; - case "format": + case string _ when verb == "format": var drivePath = cMenuExec.ItemsPath[0]; Win32API.OpenFormatDriveDialog(drivePath); break;