diff --git a/src/Greenshot.Plugin.Office/OfficeUtils.cs b/src/Greenshot.Plugin.Office/OfficeUtils.cs index 52ca8d7cb..305611c5d 100644 --- a/src/Greenshot.Plugin.Office/OfficeUtils.cs +++ b/src/Greenshot.Plugin.Office/OfficeUtils.cs @@ -1,43 +1,45 @@ using System.Linq; using Microsoft.Win32; -namespace Greenshot.Plugin.Office; - -/// -/// A small utility class for helping with office -/// -internal static class OfficeUtils +namespace Greenshot.Plugin.Office { - private static readonly string[] OfficeRootKeys = { @"SOFTWARE\Microsoft\Office", @"SOFTWARE\WOW6432Node\Microsoft\Office" }; /// - /// Get the path to the office exe + /// A small utility class for helping with office /// - /// Name of the office executable - public static string GetOfficeExePath(string exeName) + internal static class OfficeUtils { - string strKeyName = exeName switch - { - "WINWORD.EXE" => "Word", - "EXCEL.EXE" => "Excel", - "POWERPNT.EXE" => "PowerPoint", - "OUTLOOK.EXE" => "Outlook", - "ONENOTE.EXE" => "OneNote", - _ => "" - }; + private static readonly string[] OfficeRootKeys = { @"SOFTWARE\Microsoft\Office", @"SOFTWARE\WOW6432Node\Microsoft\Office" }; - foreach (string strRootKey in OfficeRootKeys) + /// + /// Get the path to the office exe + /// + /// Name of the office executable + public static string GetOfficeExePath(string exeName) { - using RegistryKey rootKey = Registry.LocalMachine.OpenSubKey(strRootKey); - if (rootKey is null) continue; - - foreach (string officeVersion in rootKey.GetSubKeyNames().Where(r => r.Contains(".")).Reverse()) + string strKeyName = exeName switch { - using RegistryKey installRootKey = Registry.LocalMachine.OpenSubKey($@"{strRootKey}\{officeVersion}\{strKeyName}\InstallRoot"); - if (installRootKey == null) continue; - return $@"{installRootKey.GetValue("Path")}\{exeName}"; + "WINWORD.EXE" => "Word", + "EXCEL.EXE" => "Excel", + "POWERPNT.EXE" => "PowerPoint", + "OUTLOOK.EXE" => "Outlook", + "ONENOTE.EXE" => "OneNote", + _ => "" + }; + + foreach (string strRootKey in OfficeRootKeys) + { + using RegistryKey rootKey = Registry.LocalMachine.OpenSubKey(strRootKey); + if (rootKey is null) continue; + + foreach (string officeVersion in rootKey.GetSubKeyNames().Where(r => r.Contains(".")).Reverse()) + { + using RegistryKey installRootKey = Registry.LocalMachine.OpenSubKey($@"{strRootKey}\{officeVersion}\{strKeyName}\InstallRoot"); + if (installRootKey == null) continue; + return $@"{installRootKey.GetValue("Path")}\{exeName}"; + } } + return null; } - return null; } -} +} \ No newline at end of file