Suggestions by Robin implemented in Code.

This commit is contained in:
jdavila71 2022-08-16 20:08:22 -04:00
commit b0c340e084
6 changed files with 52 additions and 54 deletions

View file

@ -52,41 +52,23 @@ namespace Greenshot.Base.Core
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
public static string GetOfficeExePath(string keyname, string returnValue) public static string GetOfficeExePath(string keyname)
{ {
string strKeyName = ""; string strKeyName = keyname switch
switch (keyname)
{ {
case "WINWORD.EXE": "WINWORD.EXE" => "Word",
strKeyName = "Word"; "EXCEL.EXE" => "Excel",
break; "POWERPNT.EXE" => "PowerPoint",
case "EXCEL.EXE": "OUTLOOK.EXE" => "Outlook",
strKeyName = "Excel"; "ONENOTE.EXE" => "OneNote",
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 rootKey = null;
RegistryKey officeKey; RegistryKey officeKey = null;
RegistryKey programKey; RegistryKey programKey = null;
RegistryKey installRootKey; RegistryKey installRootKey = null;
string retValue = string.Empty;
foreach (string strRootKey in strRootKeys) foreach (string strRootKey in strRootKeys)
{ {
@ -95,31 +77,47 @@ namespace Greenshot.Base.Core
if (rootKey != null) if (rootKey != null)
{ {
string[] officeVersions = rootKey.GetSubKeyNames(); 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(".")); officeKey = Registry.LocalMachine.OpenSubKey(strRootKey + "\\" + officeVersion);
Array.Reverse(officeVersions);
// string latestOfficeVersion = officeVersions.Where(r => r.Contains(".")).Max();
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;
} }
/// <summary> /// <summary>
/// Clear icon cache /// Clear icon cache

View file

@ -42,7 +42,7 @@ namespace Greenshot.Plugin.Office.Destinations
static ExcelDestination() static ExcelDestination()
{ {
ExePath = PluginUtils.GetOfficeExePath("EXCEL.EXE", "Path"); ExePath = PluginUtils.GetOfficeExePath("EXCEL.EXE");
if (ExePath == null) if (ExePath == null)
ExePath = PluginUtils.GetExePath("EXCEL.EXE"); ExePath = PluginUtils.GetExePath("EXCEL.EXE");

View file

@ -41,7 +41,7 @@ namespace Greenshot.Plugin.Office.Destinations
static OneNoteDestination() static OneNoteDestination()
{ {
exePath = PluginUtils.GetOfficeExePath("ONENOTE.EXE", "Path"); exePath = PluginUtils.GetOfficeExePath("ONENOTE.EXE");
if (exePath == null) if (exePath == null)
exePath = PluginUtils.GetExePath("ONENOTE.EXE"); exePath = PluginUtils.GetExePath("ONENOTE.EXE");
if (exePath != null && File.Exists(exePath)) if (exePath != null && File.Exists(exePath))

View file

@ -58,7 +58,7 @@ namespace Greenshot.Plugin.Office.Destinations
{ {
IsActiveFlag = true; IsActiveFlag = true;
} }
ExePath = PluginUtils.GetOfficeExePath("OUTLOOK.EXE", "Path"); ExePath = PluginUtils.GetOfficeExePath("OUTLOOK.EXE");
if (ExePath == null) if (ExePath == null)
ExePath = PluginUtils.GetExePath("OUTLOOK.EXE"); ExePath = PluginUtils.GetExePath("OUTLOOK.EXE");
if (ExePath != null && File.Exists(ExePath)) if (ExePath != null && File.Exists(ExePath))

View file

@ -45,7 +45,7 @@ namespace Greenshot.Plugin.Office.Destinations
static PowerpointDestination() static PowerpointDestination()
{ {
ExePath = PluginUtils.GetOfficeExePath("POWERPNT.EXE", "Path"); ExePath = PluginUtils.GetOfficeExePath("POWERPNT.EXE");
if (ExePath == null) if (ExePath == null)
ExePath = PluginUtils.GetExePath("POWERPNT.EXE"); ExePath = PluginUtils.GetExePath("POWERPNT.EXE");
if (ExePath != null && File.Exists(ExePath)) if (ExePath != null && File.Exists(ExePath))

View file

@ -46,7 +46,7 @@ namespace Greenshot.Plugin.Office.Destinations
static WordDestination() static WordDestination()
{ {
ExePath = PluginUtils.GetOfficeExePath("WINWORD.EXE", "Path"); ExePath = PluginUtils.GetOfficeExePath("WINWORD.EXE");
if (ExePath == null) if (ExePath == null)
ExePath = PluginUtils.GetExePath("WINWORD.EXE"); ExePath = PluginUtils.GetExePath("WINWORD.EXE");
if (ExePath != null && !File.Exists(ExePath)) if (ExePath != null && !File.Exists(ExePath))