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
{
"WINWORD.EXE" => "Word",
"EXCEL.EXE" => "Excel",
"POWERPNT.EXE" => "PowerPoint",
"OUTLOOK.EXE" => "Outlook",
"ONENOTE.EXE" => "OneNote",
_ => ""
};
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 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,8 +77,8 @@ 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(".")); officeVersions = Array.FindAll(officeVersions, r => r.Contains("."));
Array.Reverse(officeVersions); Array.Reverse(officeVersions);
// string latestOfficeVersion = officeVersions.Where(r => r.Contains(".")).Max(); // string latestOfficeVersion = officeVersions.Where(r => r.Contains(".")).Max();
@ -105,21 +87,37 @@ namespace Greenshot.Base.Core
{ {
officeKey = Registry.LocalMachine.OpenSubKey(strRootKey + "\\" + officeVersion); officeKey = Registry.LocalMachine.OpenSubKey(strRootKey + "\\" + officeVersion);
if (officeKey != null) if (officeKey is null)
{ continue;
programKey = officeKey.OpenSubKey(strKeyName); programKey = officeKey.OpenSubKey(strKeyName);
if (programKey != null)
{ if (programKey is null)
continue;
installRootKey = programKey.OpenSubKey("InstallRoot"); installRootKey = programKey.OpenSubKey("InstallRoot");
if (installRootKey != null) if (installRootKey != null)
return installRootKey.GetValue(returnValue).ToString() + "\\" + keyname; {
retValue = installRootKey.GetValue("Path").ToString() + "\\" + keyname;
break;
}
}
} }
} }
} if (rootKey != null)
} rootKey.Dispose();
} if (officeKey != null)
} officeKey.Dispose();
return string.Empty; 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))