mirror of
https://github.com/greenshot/greenshot
synced 2025-07-06 04:52:16 -07:00
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:
parent
ba8ed074c8
commit
3e88093846
9 changed files with 65 additions and 94 deletions
|
@ -7,7 +7,7 @@
|
|||
<RepositoryType>git</RepositoryType>
|
||||
<PackageProjectUrl>https://github.com/greenshot/greenshot</PackageProjectUrl>
|
||||
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
|
||||
<LangVersion>9</LangVersion>
|
||||
<LangVersion>10</LangVersion>
|
||||
<UseWPF>true</UseWPF>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<RuntimeIdentifiers>win10-x64;win10-x86;win-x64;win-x86</RuntimeIdentifiers>
|
||||
|
@ -46,7 +46,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<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>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
|
|
@ -24,6 +24,7 @@ using System.Collections.Generic;
|
|||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
using Dapplo.Windows.Icons;
|
||||
using Greenshot.Base.IniFile;
|
||||
|
@ -39,86 +40,14 @@ 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>();
|
||||
private const string PathKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\";
|
||||
|
||||
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>
|
||||
|
|
|
@ -13,8 +13,8 @@
|
|||
<PackageReference Include="Dapplo.Windows.Kernel32" Version="1.0.26" />
|
||||
<PackageReference Include="Dapplo.Windows.Multimedia" Version="1.0.26" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.11.43" />
|
||||
<PackageReference Include="log4net" version="2.0.14" />
|
||||
<PackageReference Include="Svg" Version="3.4.2" />
|
||||
<PackageReference Include="log4net" version="2.0.15" />
|
||||
<PackageReference Include="Svg" Version="3.4.3" />
|
||||
<Reference Include="Accessibility" />
|
||||
<Reference Include="CustomMarshalers" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -42,9 +42,7 @@ namespace Greenshot.Plugin.Office.Destinations
|
|||
|
||||
static ExcelDestination()
|
||||
{
|
||||
ExePath = PluginUtils.GetOfficeExePath("EXCEL.EXE");
|
||||
if (ExePath == null)
|
||||
ExePath = PluginUtils.GetExePath("EXCEL.EXE");
|
||||
ExePath = OfficeUtils.GetOfficeExePath("EXCEL.EXE") ?? PluginUtils.GetExePath("EXCEL.EXE");
|
||||
|
||||
if (ExePath != null && File.Exists(ExePath))
|
||||
{
|
||||
|
|
|
@ -41,9 +41,7 @@ namespace Greenshot.Plugin.Office.Destinations
|
|||
|
||||
static OneNoteDestination()
|
||||
{
|
||||
exePath = PluginUtils.GetOfficeExePath("ONENOTE.EXE");
|
||||
if (exePath == null)
|
||||
exePath = PluginUtils.GetExePath("ONENOTE.EXE");
|
||||
exePath = OfficeUtils.GetOfficeExePath("ONENOTE.EXE") ?? PluginUtils.GetExePath("ONENOTE.EXE");
|
||||
if (exePath != null && File.Exists(exePath))
|
||||
{
|
||||
WindowDetails.AddProcessToExcludeFromFreeze("onenote");
|
||||
|
|
|
@ -58,9 +58,7 @@ namespace Greenshot.Plugin.Office.Destinations
|
|||
{
|
||||
IsActiveFlag = true;
|
||||
}
|
||||
ExePath = PluginUtils.GetOfficeExePath("OUTLOOK.EXE");
|
||||
if (ExePath == null)
|
||||
ExePath = PluginUtils.GetExePath("OUTLOOK.EXE");
|
||||
ExePath = OfficeUtils.GetOfficeExePath("OUTLOOK.EXE") ?? PluginUtils.GetExePath("OUTLOOK.EXE");
|
||||
if (ExePath != null && File.Exists(ExePath))
|
||||
{
|
||||
WindowDetails.AddProcessToExcludeFromFreeze("outlook");
|
||||
|
|
|
@ -45,9 +45,7 @@ namespace Greenshot.Plugin.Office.Destinations
|
|||
|
||||
static PowerpointDestination()
|
||||
{
|
||||
ExePath = PluginUtils.GetOfficeExePath("POWERPNT.EXE");
|
||||
if (ExePath == null)
|
||||
ExePath = PluginUtils.GetExePath("POWERPNT.EXE");
|
||||
ExePath = OfficeUtils.GetOfficeExePath("POWERPNT.EXE") ?? PluginUtils.GetExePath("POWERPNT.EXE");
|
||||
if (ExePath != null && File.Exists(ExePath))
|
||||
{
|
||||
WindowDetails.AddProcessToExcludeFromFreeze("powerpnt");
|
||||
|
|
|
@ -46,9 +46,7 @@ namespace Greenshot.Plugin.Office.Destinations
|
|||
|
||||
static WordDestination()
|
||||
{
|
||||
ExePath = PluginUtils.GetOfficeExePath("WINWORD.EXE");
|
||||
if (ExePath == null)
|
||||
ExePath = PluginUtils.GetExePath("WINWORD.EXE");
|
||||
ExePath = OfficeUtils.GetOfficeExePath("WINWORD.EXE") ?? PluginUtils.GetExePath("WINWORD.EXE");
|
||||
if (ExePath != null && !File.Exists(ExePath))
|
||||
{
|
||||
ExePath = null;
|
||||
|
@ -120,7 +118,7 @@ namespace Greenshot.Plugin.Office.Destinations
|
|||
if (!manuallyInitiated)
|
||||
{
|
||||
var documents = _wordExporter.GetWordDocuments().ToList();
|
||||
if (documents != null && documents.Count > 0)
|
||||
if (documents is { Count: > 0 })
|
||||
{
|
||||
var destinations = new List<IDestination>
|
||||
{
|
||||
|
|
52
src/Greenshot.Plugin.Office/OfficeUtils.cs
Normal file
52
src/Greenshot.Plugin.Office/OfficeUtils.cs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue