From 3e8809384692a713eb42f6b992cd1c4f43808a28 Mon Sep 17 00:00:00 2001 From: Robin Krom Date: Wed, 17 Aug 2022 23:01:24 +0200 Subject: [PATCH] Some aftercare for #431, moved the code to find the path for the office executables to the office plugin, as this is where it's needed. Also optimized the code a bit, using modern features. --- src/Directory.Build.props | 4 +- src/Greenshot.Base/Core/PluginUtils.cs | 77 +------------------ src/Greenshot.Base/Greenshot.Base.csproj | 4 +- .../Destinations/ExcelDestination.cs | 4 +- .../Destinations/OneNoteDestination.cs | 4 +- .../Destinations/OutlookDestination.cs | 4 +- .../Destinations/PowerpointDestination.cs | 4 +- .../Destinations/WordDestination.cs | 6 +- src/Greenshot.Plugin.Office/OfficeUtils.cs | 52 +++++++++++++ 9 files changed, 65 insertions(+), 94 deletions(-) create mode 100644 src/Greenshot.Plugin.Office/OfficeUtils.cs diff --git a/src/Directory.Build.props b/src/Directory.Build.props index f5b9f2e1c..2fbce49ed 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -7,7 +7,7 @@ git https://github.com/greenshot/greenshot GPL-3.0-only - 9 + 10 true true win10-x64;win10-x86;win-x64;win-x86 @@ -46,7 +46,7 @@ - + all runtime; build; native; contentfiles; analyzers diff --git a/src/Greenshot.Base/Core/PluginUtils.cs b/src/Greenshot.Base/Core/PluginUtils.cs index 6a2755d50..76f08422b 100644 --- a/src/Greenshot.Base/Core/PluginUtils.cs +++ b/src/Greenshot.Base/Core/PluginUtils.cs @@ -24,6 +24,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.IO; +using System.Linq; using System.Windows.Forms; using Dapplo.Windows.Icons; using Greenshot.Base.IniFile; @@ -39,86 +40,14 @@ namespace Greenshot.Base.Core { private static readonly ILog Log = LogManager.GetLogger(typeof(PluginUtils)); private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection(); - private const string PathKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\"; - private static string[] strRootKeys = { @"SOFTWARE\Microsoft\Office", @"SOFTWARE\WOW6432Node\Microsoft\Office" }; private static readonly IDictionary ExeIconCache = new Dictionary(); - + private const string PathKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\"; + static PluginUtils() { CoreConfig.PropertyChanged += OnIconSizeChanged; } - /// - /// Clear icon cache - /// - /// - /// - public static string GetOfficeExePath(string keyname) - { - string strKeyName = keyname switch - { - "WINWORD.EXE" => "Word", - "EXCEL.EXE" => "Excel", - "POWERPNT.EXE" => "PowerPoint", - "OUTLOOK.EXE" => "Outlook", - "ONENOTE.EXE" => "OneNote", - _ => "" - }; - RegistryKey rootKey = null; - RegistryKey officeKey = null; - RegistryKey programKey = null; - RegistryKey installRootKey = null; - string retValue = string.Empty; - - foreach (string strRootKey in strRootKeys) - { - rootKey = Registry.LocalMachine.OpenSubKey(strRootKey); - - if (rootKey != null) - { - string[] officeVersions = rootKey.GetSubKeyNames(); - if (officeVersions is null) - continue; - officeVersions = Array.FindAll(officeVersions, r => r.Contains(".")); - Array.Reverse(officeVersions); - // string latestOfficeVersion = officeVersions.Where(r => r.Contains(".")).Max(); - - foreach (string officeVersion in officeVersions) - { - officeKey = Registry.LocalMachine.OpenSubKey(strRootKey + "\\" + officeVersion); - - if (officeKey is null) - continue; - - programKey = officeKey.OpenSubKey(strKeyName); - - if (programKey is null) - continue; - - installRootKey = programKey.OpenSubKey("InstallRoot"); - - if (installRootKey != null) - { - retValue = installRootKey.GetValue("Path").ToString() + "\\" + keyname; - break; - - } - - } - - - } - } - if (rootKey != null) - rootKey.Dispose(); - if (officeKey != null) - officeKey.Dispose(); - if (programKey != null) - programKey.Dispose(); - if (installRootKey != null) - installRootKey.Dispose(); - return retValue; - } /// /// Clear icon cache /// diff --git a/src/Greenshot.Base/Greenshot.Base.csproj b/src/Greenshot.Base/Greenshot.Base.csproj index a698b8e0b..021ea53ec 100644 --- a/src/Greenshot.Base/Greenshot.Base.csproj +++ b/src/Greenshot.Base/Greenshot.Base.csproj @@ -13,8 +13,8 @@ - - + + diff --git a/src/Greenshot.Plugin.Office/Destinations/ExcelDestination.cs b/src/Greenshot.Plugin.Office/Destinations/ExcelDestination.cs index 8c13e4754..1a671cf7a 100644 --- a/src/Greenshot.Plugin.Office/Destinations/ExcelDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/ExcelDestination.cs @@ -42,9 +42,7 @@ namespace Greenshot.Plugin.Office.Destinations static ExcelDestination() { - ExePath = PluginUtils.GetOfficeExePath("EXCEL.EXE"); - if (ExePath == null) - ExePath = PluginUtils.GetExePath("EXCEL.EXE"); + ExePath = OfficeUtils.GetOfficeExePath("EXCEL.EXE") ?? PluginUtils.GetExePath("EXCEL.EXE"); if (ExePath != null && File.Exists(ExePath)) { diff --git a/src/Greenshot.Plugin.Office/Destinations/OneNoteDestination.cs b/src/Greenshot.Plugin.Office/Destinations/OneNoteDestination.cs index a2ab24937..5f8da7a67 100644 --- a/src/Greenshot.Plugin.Office/Destinations/OneNoteDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/OneNoteDestination.cs @@ -41,9 +41,7 @@ namespace Greenshot.Plugin.Office.Destinations static OneNoteDestination() { - exePath = PluginUtils.GetOfficeExePath("ONENOTE.EXE"); - if (exePath == null) - exePath = PluginUtils.GetExePath("ONENOTE.EXE"); + exePath = OfficeUtils.GetOfficeExePath("ONENOTE.EXE") ?? PluginUtils.GetExePath("ONENOTE.EXE"); if (exePath != null && File.Exists(exePath)) { WindowDetails.AddProcessToExcludeFromFreeze("onenote"); diff --git a/src/Greenshot.Plugin.Office/Destinations/OutlookDestination.cs b/src/Greenshot.Plugin.Office/Destinations/OutlookDestination.cs index 68cdd706b..3c77ea4f1 100644 --- a/src/Greenshot.Plugin.Office/Destinations/OutlookDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/OutlookDestination.cs @@ -58,9 +58,7 @@ namespace Greenshot.Plugin.Office.Destinations { IsActiveFlag = true; } - ExePath = PluginUtils.GetOfficeExePath("OUTLOOK.EXE"); - if (ExePath == null) - ExePath = PluginUtils.GetExePath("OUTLOOK.EXE"); + ExePath = OfficeUtils.GetOfficeExePath("OUTLOOK.EXE") ?? PluginUtils.GetExePath("OUTLOOK.EXE"); if (ExePath != null && File.Exists(ExePath)) { WindowDetails.AddProcessToExcludeFromFreeze("outlook"); diff --git a/src/Greenshot.Plugin.Office/Destinations/PowerpointDestination.cs b/src/Greenshot.Plugin.Office/Destinations/PowerpointDestination.cs index 34f734cc4..010093027 100644 --- a/src/Greenshot.Plugin.Office/Destinations/PowerpointDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/PowerpointDestination.cs @@ -45,9 +45,7 @@ namespace Greenshot.Plugin.Office.Destinations static PowerpointDestination() { - ExePath = PluginUtils.GetOfficeExePath("POWERPNT.EXE"); - if (ExePath == null) - ExePath = PluginUtils.GetExePath("POWERPNT.EXE"); + ExePath = OfficeUtils.GetOfficeExePath("POWERPNT.EXE") ?? PluginUtils.GetExePath("POWERPNT.EXE"); if (ExePath != null && File.Exists(ExePath)) { WindowDetails.AddProcessToExcludeFromFreeze("powerpnt"); diff --git a/src/Greenshot.Plugin.Office/Destinations/WordDestination.cs b/src/Greenshot.Plugin.Office/Destinations/WordDestination.cs index 34dfebad7..d6220a302 100644 --- a/src/Greenshot.Plugin.Office/Destinations/WordDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/WordDestination.cs @@ -46,9 +46,7 @@ namespace Greenshot.Plugin.Office.Destinations static WordDestination() { - ExePath = PluginUtils.GetOfficeExePath("WINWORD.EXE"); - if (ExePath == null) - ExePath = PluginUtils.GetExePath("WINWORD.EXE"); + ExePath = OfficeUtils.GetOfficeExePath("WINWORD.EXE") ?? PluginUtils.GetExePath("WINWORD.EXE"); if (ExePath != null && !File.Exists(ExePath)) { ExePath = null; @@ -120,7 +118,7 @@ namespace Greenshot.Plugin.Office.Destinations if (!manuallyInitiated) { var documents = _wordExporter.GetWordDocuments().ToList(); - if (documents != null && documents.Count > 0) + if (documents is { Count: > 0 }) { var destinations = new List { diff --git a/src/Greenshot.Plugin.Office/OfficeUtils.cs b/src/Greenshot.Plugin.Office/OfficeUtils.cs new file mode 100644 index 000000000..770f8b1e8 --- /dev/null +++ b/src/Greenshot.Plugin.Office/OfficeUtils.cs @@ -0,0 +1,52 @@ +using System.Linq; +using Microsoft.Win32; + +namespace Greenshot.Plugin.Office; + +/// +/// A small utility class for helping with office +/// +internal static class OfficeUtils +{ + private static readonly string[] OfficeRootKeys = { @"SOFTWARE\Microsoft\Office", @"SOFTWARE\WOW6432Node\Microsoft\Office" }; + + /// + /// Get the path to the office exe + /// + /// Name of the office executable + public static string GetOfficeExePath(string exeName) + { + string strKeyName = exeName switch + { + "WINWORD.EXE" => "Word", + "EXCEL.EXE" => "Excel", + "POWERPNT.EXE" => "PowerPoint", + "OUTLOOK.EXE" => "Outlook", + "ONENOTE.EXE" => "OneNote", + _ => "" + }; + + foreach (string strRootKey in OfficeRootKeys) + { + using RegistryKey rootKey = Registry.LocalMachine.OpenSubKey(strRootKey); + + if (rootKey is null) continue; + + + foreach (string officeVersion in rootKey.GetSubKeyNames().Where(r => r.Contains(".")).Reverse()) + { + using RegistryKey officeKey = Registry.LocalMachine.OpenSubKey(strRootKey + "\\" + officeVersion); + if (officeKey is null) continue; + + using RegistryKey programKey = officeKey.OpenSubKey(strKeyName); + if (programKey is null) continue; + + using RegistryKey installRootKey = programKey.OpenSubKey("InstallRoot"); + if (installRootKey == null) continue; + + return installRootKey.GetValue("Path") + "\\" + exeName; + } + } + return string.Empty; + } +}