From fb9a0c2a248fce4a12eb977eea156e43e6adde78 Mon Sep 17 00:00:00 2001 From: jdavila71 Date: Mon, 15 Aug 2022 21:55:07 -0400 Subject: [PATCH] Fix of BUG2951 --- src/Greenshot.Base/Core/PluginUtils.cs | 76 ++++++++++++++++++- .../Destinations/ExcelDestination.cs | 5 +- .../Destinations/OneNoteDestination.cs | 4 +- .../Destinations/OutlookDestination.cs | 5 +- .../Destinations/PowerpointDestination.cs | 4 +- .../Destinations/WordDestination.cs | 4 +- 6 files changed, 91 insertions(+), 7 deletions(-) diff --git a/src/Greenshot.Base/Core/PluginUtils.cs b/src/Greenshot.Base/Core/PluginUtils.cs index d41e545a5..8dc35d8cf 100644 --- a/src/Greenshot.Base/Core/PluginUtils.cs +++ b/src/Greenshot.Base/Core/PluginUtils.cs @@ -40,13 +40,87 @@ 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(); static PluginUtils() { CoreConfig.PropertyChanged += OnIconSizeChanged; } + /// + /// Clear icon cache + /// + /// + /// + public static string GetOfficeExePath(string keyname, string returnValue) + { + string strKeyName = ""; + switch (keyname) + { + case "WINWORD.EXE": + strKeyName = "Word"; + break; + case "EXCEL.EXE": + strKeyName = "Excel"; + break; + case "MSACCESS.EXE": + strKeyName = "Access"; + break; + case "POWERPNT.EXE": + strKeyName = "PowerPoint"; + break; + case "OUTLOOK.EXE": + strKeyName = "Outlook"; + break; + case "ONENOTE.EXE": + strKeyName = "OneNote"; + break; + case "MSPUB.EXE": + strKeyName = "Publisher"; + break; + default: + strKeyName = ""; + break; + } + RegistryKey rootKey = null; + RegistryKey officeKey; + RegistryKey programKey; + RegistryKey installRootKey; + + foreach (string strRootKey in strRootKeys) + { + rootKey = Registry.LocalMachine.OpenSubKey(strRootKey); + + if (rootKey != null) + { + string[] officeVersions = rootKey.GetSubKeyNames(); + if (officeVersions != null) + { + 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 != null) + { + programKey = officeKey.OpenSubKey(strKeyName); + if (programKey != null) + { + installRootKey = programKey.OpenSubKey("InstallRoot"); + if (installRootKey != null) + return installRootKey.GetValue(returnValue).ToString() + "\\" + keyname; + } + } + } + } + } + } + return string.Empty; + } /// /// Clear icon cache /// @@ -84,7 +158,7 @@ namespace Greenshot.Base.Core if (key != null) { // "" is the default key, which should point to the requested location - return (string) key.GetValue(string.Empty); + return (string)key.GetValue(string.Empty); } } diff --git a/src/Greenshot.Plugin.Office/Destinations/ExcelDestination.cs b/src/Greenshot.Plugin.Office/Destinations/ExcelDestination.cs index fb1930c8c..89b4b3244 100644 --- a/src/Greenshot.Plugin.Office/Destinations/ExcelDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/ExcelDestination.cs @@ -42,7 +42,10 @@ namespace Greenshot.Plugin.Office.Destinations static ExcelDestination() { - ExePath = PluginUtils.GetExePath("EXCEL.EXE"); + ExePath = PluginUtils.GetOfficeExePath("EXCEL.EXE", "Path"); + if (ExePath == null) + ExePath = PluginUtils.GetExePath("EXCEL.EXE"); + if (ExePath != null && File.Exists(ExePath)) { WindowDetails.AddProcessToExcludeFromFreeze("excel"); diff --git a/src/Greenshot.Plugin.Office/Destinations/OneNoteDestination.cs b/src/Greenshot.Plugin.Office/Destinations/OneNoteDestination.cs index 387810494..261c6d8e3 100644 --- a/src/Greenshot.Plugin.Office/Destinations/OneNoteDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/OneNoteDestination.cs @@ -41,7 +41,9 @@ namespace Greenshot.Plugin.Office.Destinations static OneNoteDestination() { - exePath = PluginUtils.GetExePath("ONENOTE.EXE"); + exePath = PluginUtils.GetOfficeExePath("ONENOTE.EXE", "Path"); + if (exePath == null) + exePath = 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 6d93c4096..fb3a9fc3e 100644 --- a/src/Greenshot.Plugin.Office/Destinations/OutlookDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/OutlookDestination.cs @@ -58,8 +58,9 @@ namespace Greenshot.Plugin.Office.Destinations { IsActiveFlag = true; } - - ExePath = PluginUtils.GetExePath("OUTLOOK.EXE"); + ExePath = PluginUtils.GetOfficeExePath("OUTLOOK.EXE", "Path"); + if (ExePath == null) + ExePath = 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 3b9ce4bc8..3892565d5 100644 --- a/src/Greenshot.Plugin.Office/Destinations/PowerpointDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/PowerpointDestination.cs @@ -45,7 +45,9 @@ namespace Greenshot.Plugin.Office.Destinations static PowerpointDestination() { - ExePath = PluginUtils.GetExePath("POWERPNT.EXE"); + ExePath = PluginUtils.GetOfficeExePath("POWERPNT.EXE", "Path"); + if (ExePath == null) + ExePath = 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 99cf4693c..5fa3bc430 100644 --- a/src/Greenshot.Plugin.Office/Destinations/WordDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/WordDestination.cs @@ -46,7 +46,9 @@ namespace Greenshot.Plugin.Office.Destinations static WordDestination() { - ExePath = PluginUtils.GetExePath("WINWORD.EXE"); + ExePath = PluginUtils.GetOfficeExePath("WINWORD.EXE", "Path"); + if (ExePath == null) + ExePath = PluginUtils.GetExePath("WINWORD.EXE"); if (ExePath != null && !File.Exists(ExePath)) { ExePath = null;