diff --git a/src/Greenshot.Base/Core/PluginUtils.cs b/src/Greenshot.Base/Core/PluginUtils.cs index d41e545a5..6a2755d50 100644 --- a/src/Greenshot.Base/Core/PluginUtils.cs +++ b/src/Greenshot.Base/Core/PluginUtils.cs @@ -40,13 +40,85 @@ 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 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 /// @@ -84,7 +156,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..8c13e4754 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"); + 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..a2ab24937 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"); + 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..68cdd706b 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"); + 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..34f734cc4 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"); + 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..34dfebad7 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"); + if (ExePath == null) + ExePath = PluginUtils.GetExePath("WINWORD.EXE"); if (ExePath != null && !File.Exists(ExePath)) { ExePath = null;