Fix of BUG-2951, issues with finding Office installations (#431)

This commit is contained in:
jdavila71 2022-08-17 16:01:01 -04:00 committed by GitHub
parent 2b5e45e33e
commit ba8ed074c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 89 additions and 7 deletions

View file

@ -40,13 +40,85 @@ namespace Greenshot.Base.Core
private static readonly ILog Log = LogManager.GetLogger(typeof(PluginUtils)); private static readonly ILog Log = LogManager.GetLogger(typeof(PluginUtils));
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>(); private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
private const string PathKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\"; 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<string, Image> ExeIconCache = new Dictionary<string, Image>(); private static readonly IDictionary<string, Image> ExeIconCache = new Dictionary<string, Image>();
static PluginUtils() static PluginUtils()
{ {
CoreConfig.PropertyChanged += OnIconSizeChanged; CoreConfig.PropertyChanged += OnIconSizeChanged;
} }
/// <summary>
/// Clear icon cache
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
/// <summary> /// <summary>
/// Clear icon cache /// Clear icon cache
/// </summary> /// </summary>
@ -84,7 +156,7 @@ namespace Greenshot.Base.Core
if (key != null) if (key != null)
{ {
// "" is the default key, which should point to the requested location // "" is the default key, which should point to the requested location
return (string) key.GetValue(string.Empty); return (string)key.GetValue(string.Empty);
} }
} }

View file

@ -42,7 +42,10 @@ namespace Greenshot.Plugin.Office.Destinations
static ExcelDestination() 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)) if (ExePath != null && File.Exists(ExePath))
{ {
WindowDetails.AddProcessToExcludeFromFreeze("excel"); WindowDetails.AddProcessToExcludeFromFreeze("excel");

View file

@ -41,7 +41,9 @@ namespace Greenshot.Plugin.Office.Destinations
static OneNoteDestination() 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)) if (exePath != null && File.Exists(exePath))
{ {
WindowDetails.AddProcessToExcludeFromFreeze("onenote"); WindowDetails.AddProcessToExcludeFromFreeze("onenote");

View file

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

View file

@ -45,7 +45,9 @@ namespace Greenshot.Plugin.Office.Destinations
static PowerpointDestination() 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)) if (ExePath != null && File.Exists(ExePath))
{ {
WindowDetails.AddProcessToExcludeFromFreeze("powerpnt"); WindowDetails.AddProcessToExcludeFromFreeze("powerpnt");

View file

@ -46,7 +46,9 @@ namespace Greenshot.Plugin.Office.Destinations
static WordDestination() 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)) if (ExePath != null && !File.Exists(ExePath))
{ {
ExePath = null; ExePath = null;