Some aftercare for #431, moved the code to find the path for the office executables to the office plugin, as this is where it's needed. Also optimized the code a bit, using modern features.

This commit is contained in:
Robin Krom 2022-08-17 23:01:24 +02:00
parent ba8ed074c8
commit 3e88093846
No known key found for this signature in database
GPG key ID: BCC01364F1371490
9 changed files with 65 additions and 94 deletions

View file

@ -7,7 +7,7 @@
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://github.com/greenshot/greenshot</PackageProjectUrl> <PackageProjectUrl>https://github.com/greenshot/greenshot</PackageProjectUrl>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression> <PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
<LangVersion>9</LangVersion> <LangVersion>10</LangVersion>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms> <UseWindowsForms>true</UseWindowsForms>
<RuntimeIdentifiers>win10-x64;win10-x86;win-x64;win-x86</RuntimeIdentifiers> <RuntimeIdentifiers>win10-x64;win10-x86;win-x64;win-x86</RuntimeIdentifiers>
@ -46,7 +46,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition="!$(MSBuildProjectName.Contains('Tests')) And $(MSBuildProjectName.StartsWith('Greenshot'))"> <ItemGroup Condition="!$(MSBuildProjectName.Contains('Tests')) And $(MSBuildProjectName.StartsWith('Greenshot'))">
<PackageReference Include="Nerdbank.GitVersioning" Version="3.5.107"> <PackageReference Include="Nerdbank.GitVersioning" Version="3.5.109">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference> </PackageReference>

View file

@ -24,6 +24,7 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using Dapplo.Windows.Icons; using Dapplo.Windows.Icons;
using Greenshot.Base.IniFile; using Greenshot.Base.IniFile;
@ -39,86 +40,14 @@ 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 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>();
private const string PathKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\";
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>

View file

@ -13,8 +13,8 @@
<PackageReference Include="Dapplo.Windows.Kernel32" Version="1.0.26" /> <PackageReference Include="Dapplo.Windows.Kernel32" Version="1.0.26" />
<PackageReference Include="Dapplo.Windows.Multimedia" Version="1.0.26" /> <PackageReference Include="Dapplo.Windows.Multimedia" Version="1.0.26" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.43" /> <PackageReference Include="HtmlAgilityPack" Version="1.11.43" />
<PackageReference Include="log4net" version="2.0.14" /> <PackageReference Include="log4net" version="2.0.15" />
<PackageReference Include="Svg" Version="3.4.2" /> <PackageReference Include="Svg" Version="3.4.3" />
<Reference Include="Accessibility" /> <Reference Include="Accessibility" />
<Reference Include="CustomMarshalers" /> <Reference Include="CustomMarshalers" />
</ItemGroup> </ItemGroup>

View file

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

View file

@ -41,9 +41,7 @@ namespace Greenshot.Plugin.Office.Destinations
static OneNoteDestination() static OneNoteDestination()
{ {
exePath = PluginUtils.GetOfficeExePath("ONENOTE.EXE"); exePath = OfficeUtils.GetOfficeExePath("ONENOTE.EXE") ?? PluginUtils.GetExePath("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,9 +58,7 @@ namespace Greenshot.Plugin.Office.Destinations
{ {
IsActiveFlag = true; IsActiveFlag = true;
} }
ExePath = PluginUtils.GetOfficeExePath("OUTLOOK.EXE"); ExePath = OfficeUtils.GetOfficeExePath("OUTLOOK.EXE") ?? 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,9 +45,7 @@ namespace Greenshot.Plugin.Office.Destinations
static PowerpointDestination() static PowerpointDestination()
{ {
ExePath = PluginUtils.GetOfficeExePath("POWERPNT.EXE"); ExePath = OfficeUtils.GetOfficeExePath("POWERPNT.EXE") ?? PluginUtils.GetExePath("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,9 +46,7 @@ namespace Greenshot.Plugin.Office.Destinations
static WordDestination() static WordDestination()
{ {
ExePath = PluginUtils.GetOfficeExePath("WINWORD.EXE"); ExePath = OfficeUtils.GetOfficeExePath("WINWORD.EXE") ?? PluginUtils.GetExePath("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;
@ -120,7 +118,7 @@ namespace Greenshot.Plugin.Office.Destinations
if (!manuallyInitiated) if (!manuallyInitiated)
{ {
var documents = _wordExporter.GetWordDocuments().ToList(); var documents = _wordExporter.GetWordDocuments().ToList();
if (documents != null && documents.Count > 0) if (documents is { Count: > 0 })
{ {
var destinations = new List<IDestination> var destinations = new List<IDestination>
{ {

View file

@ -0,0 +1,52 @@
using System.Linq;
using Microsoft.Win32;
namespace Greenshot.Plugin.Office;
/// <summary>
/// A small utility class for helping with office
/// </summary>
internal static class OfficeUtils
{
private static readonly string[] OfficeRootKeys = { @"SOFTWARE\Microsoft\Office", @"SOFTWARE\WOW6432Node\Microsoft\Office" };
/// <summary>
/// Get the path to the office exe
/// </summary>
/// <param name="exeName">Name of the office executable</param>
public static string GetOfficeExePath(string exeName)
{
string strKeyName = exeName switch
{
"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 officeKey = Registry.LocalMachine.OpenSubKey(strRootKey + "\\" + officeVersion);
if (officeKey is null) continue;
using RegistryKey programKey = officeKey.OpenSubKey(strKeyName);
if (programKey is null) continue;
using RegistryKey installRootKey = programKey.OpenSubKey("InstallRoot");
if (installRootKey == null) continue;
return installRootKey.GetValue("Path") + "\\" + exeName;
}
}
return string.Empty;
}
}