Refactored PluginUtils to have the GetExePath and GetExeIcon, which was needed for giving the ExternalCommandPlugin an icon.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2057 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-09-19 10:05:26 +00:00
commit 91385b074e
13 changed files with 119 additions and 83 deletions

View file

@ -23,13 +23,60 @@ using System.Drawing;
using System.Windows.Forms;
using Greenshot.Plugin;
using Microsoft.Win32;
using System.IO;
namespace GreenshotPlugin.Core {
/// <summary>
/// Description of PluginUtils.
/// </summary>
public static class PluginUtils {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PluginUtils));
private const string PATH_KEY = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\";
/// <summary>
/// Get the path of an executable
/// </summary>
/// <param name="exeName">e.g. cmd.exe</param>
/// <returns>Path to file</returns>
public static string GetExePath(string exeName) {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(PATH_KEY + exeName, false)) {
if (key != null) {
// "" is the default key, which should point to the requested location
return (string)key.GetValue("");
}
}
foreach (string test in (Environment.GetEnvironmentVariable("PATH") ?? "").Split(';')) {
string path = test.Trim();
if (!String.IsNullOrEmpty(path) && File.Exists(path = Path.Combine(path, exeName))) {
return Path.GetFullPath(path);
}
}
return null;
}
/// <summary>
/// Get icon for executable
/// </summary>
/// <param name="path">path to the exe or dll</param>
/// <param name="index">index of the icon</param>
/// <returns>Bitmap with the icon or null if something happended</returns>
public static Bitmap GetExeIcon(string path, int index) {
if (!File.Exists(path)) {
return null;
}
try {
using (Icon appIcon = ImageHelper.ExtractAssociatedIcon(path, index, false)) {
if (appIcon != null) {
return appIcon.ToBitmap();
}
}
} catch (Exception exIcon) {
LOG.Error("error retrieving icon: ", exIcon);
}
return null;
}
/// <summary>
/// Helper method to add a MenuItem to the File MenuItem of an ImageEditor
/// </summary>