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 CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
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>();
static PluginUtils()
{
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>
/// Clear icon cache
/// </summary>

View file

@ -42,7 +42,10 @@ namespace Greenshot.Plugin.Office.Destinations
static ExcelDestination()
{
ExePath = PluginUtils.GetOfficeExePath("EXCEL.EXE");
if (ExePath == null)
ExePath = PluginUtils.GetExePath("EXCEL.EXE");
if (ExePath != null && File.Exists(ExePath))
{
WindowDetails.AddProcessToExcludeFromFreeze("excel");

View file

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

View file

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

View file

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

View file

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