From b0c340e084486ff32632b47e71be294b57713a85 Mon Sep 17 00:00:00 2001 From: jdavila71 Date: Tue, 16 Aug 2022 20:08:22 -0400 Subject: [PATCH] Suggestions by Robin implemented in Code. --- src/Greenshot.Base/Core/PluginUtils.cs | 96 +++++++++---------- .../Destinations/ExcelDestination.cs | 2 +- .../Destinations/OneNoteDestination.cs | 2 +- .../Destinations/OutlookDestination.cs | 2 +- .../Destinations/PowerpointDestination.cs | 2 +- .../Destinations/WordDestination.cs | 2 +- 6 files changed, 52 insertions(+), 54 deletions(-) diff --git a/src/Greenshot.Base/Core/PluginUtils.cs b/src/Greenshot.Base/Core/PluginUtils.cs index 8dc35d8cf..6a2755d50 100644 --- a/src/Greenshot.Base/Core/PluginUtils.cs +++ b/src/Greenshot.Base/Core/PluginUtils.cs @@ -52,41 +52,23 @@ namespace Greenshot.Base.Core /// /// /// - public static string GetOfficeExePath(string keyname, string returnValue) + public static string GetOfficeExePath(string keyname) { - string strKeyName = ""; - - switch (keyname) + string strKeyName = keyname switch { - 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; - } + "WINWORD.EXE" => "Word", + "EXCEL.EXE" => "Excel", + "POWERPNT.EXE" => "PowerPoint", + "OUTLOOK.EXE" => "Outlook", + "ONENOTE.EXE" => "OneNote", + _ => "" + }; + RegistryKey rootKey = null; - RegistryKey officeKey; - RegistryKey programKey; - RegistryKey installRootKey; + RegistryKey officeKey = null; + RegistryKey programKey = null; + RegistryKey installRootKey = null; + string retValue = string.Empty; foreach (string strRootKey in strRootKeys) { @@ -95,31 +77,47 @@ namespace Greenshot.Base.Core if (rootKey != null) { string[] officeVersions = rootKey.GetSubKeyNames(); - if (officeVersions != null) + 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) { - officeVersions = Array.FindAll(officeVersions, r => r.Contains(".")); - Array.Reverse(officeVersions); - // string latestOfficeVersion = officeVersions.Where(r => r.Contains(".")).Max(); + officeKey = Registry.LocalMachine.OpenSubKey(strRootKey + "\\" + officeVersion); - foreach (string officeVersion in officeVersions) + if (officeKey is null) + continue; + + programKey = officeKey.OpenSubKey(strKeyName); + + if (programKey is null) + continue; + + installRootKey = programKey.OpenSubKey("InstallRoot"); + + if (installRootKey != null) { - officeKey = Registry.LocalMachine.OpenSubKey(strRootKey + "\\" + officeVersion); + retValue = installRootKey.GetValue("Path").ToString() + "\\" + keyname; + break; - 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; + 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.Plugin.Office/Destinations/ExcelDestination.cs b/src/Greenshot.Plugin.Office/Destinations/ExcelDestination.cs index 89b4b3244..8c13e4754 100644 --- a/src/Greenshot.Plugin.Office/Destinations/ExcelDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/ExcelDestination.cs @@ -42,7 +42,7 @@ namespace Greenshot.Plugin.Office.Destinations static ExcelDestination() { - ExePath = PluginUtils.GetOfficeExePath("EXCEL.EXE", "Path"); + ExePath = PluginUtils.GetOfficeExePath("EXCEL.EXE"); if (ExePath == null) ExePath = PluginUtils.GetExePath("EXCEL.EXE"); diff --git a/src/Greenshot.Plugin.Office/Destinations/OneNoteDestination.cs b/src/Greenshot.Plugin.Office/Destinations/OneNoteDestination.cs index 261c6d8e3..a2ab24937 100644 --- a/src/Greenshot.Plugin.Office/Destinations/OneNoteDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/OneNoteDestination.cs @@ -41,7 +41,7 @@ namespace Greenshot.Plugin.Office.Destinations static OneNoteDestination() { - exePath = PluginUtils.GetOfficeExePath("ONENOTE.EXE", "Path"); + exePath = PluginUtils.GetOfficeExePath("ONENOTE.EXE"); if (exePath == null) exePath = PluginUtils.GetExePath("ONENOTE.EXE"); if (exePath != null && File.Exists(exePath)) diff --git a/src/Greenshot.Plugin.Office/Destinations/OutlookDestination.cs b/src/Greenshot.Plugin.Office/Destinations/OutlookDestination.cs index fb3a9fc3e..68cdd706b 100644 --- a/src/Greenshot.Plugin.Office/Destinations/OutlookDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/OutlookDestination.cs @@ -58,7 +58,7 @@ namespace Greenshot.Plugin.Office.Destinations { IsActiveFlag = true; } - ExePath = PluginUtils.GetOfficeExePath("OUTLOOK.EXE", "Path"); + ExePath = PluginUtils.GetOfficeExePath("OUTLOOK.EXE"); if (ExePath == null) ExePath = PluginUtils.GetExePath("OUTLOOK.EXE"); if (ExePath != null && File.Exists(ExePath)) diff --git a/src/Greenshot.Plugin.Office/Destinations/PowerpointDestination.cs b/src/Greenshot.Plugin.Office/Destinations/PowerpointDestination.cs index 3892565d5..34f734cc4 100644 --- a/src/Greenshot.Plugin.Office/Destinations/PowerpointDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/PowerpointDestination.cs @@ -45,7 +45,7 @@ namespace Greenshot.Plugin.Office.Destinations static PowerpointDestination() { - ExePath = PluginUtils.GetOfficeExePath("POWERPNT.EXE", "Path"); + ExePath = PluginUtils.GetOfficeExePath("POWERPNT.EXE"); if (ExePath == null) ExePath = PluginUtils.GetExePath("POWERPNT.EXE"); if (ExePath != null && File.Exists(ExePath)) diff --git a/src/Greenshot.Plugin.Office/Destinations/WordDestination.cs b/src/Greenshot.Plugin.Office/Destinations/WordDestination.cs index 5fa3bc430..34dfebad7 100644 --- a/src/Greenshot.Plugin.Office/Destinations/WordDestination.cs +++ b/src/Greenshot.Plugin.Office/Destinations/WordDestination.cs @@ -46,7 +46,7 @@ namespace Greenshot.Plugin.Office.Destinations static WordDestination() { - ExePath = PluginUtils.GetOfficeExePath("WINWORD.EXE", "Path"); + ExePath = PluginUtils.GetOfficeExePath("WINWORD.EXE"); if (ExePath == null) ExePath = PluginUtils.GetExePath("WINWORD.EXE"); if (ExePath != null && !File.Exists(ExePath))