2019-09-05 06:05:17 +08:00
|
|
|
using System;
|
2016-08-20 06:05:59 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2016-11-29 09:55:25 +08:00
|
|
|
using System.Runtime.InteropServices;
|
2017-03-02 07:21:34 +08:00
|
|
|
using System.Security;
|
2016-08-21 01:18:41 +08:00
|
|
|
using System.Text;
|
2016-08-21 00:08:16 +08:00
|
|
|
using Microsoft.Win32;
|
2016-08-21 01:18:41 +08:00
|
|
|
using Shell;
|
2016-08-22 09:21:22 +08:00
|
|
|
using Wox.Infrastructure;
|
2019-10-25 10:11:52 +08:00
|
|
|
using Wox.Plugin.Program.Logger;
|
2016-08-20 06:05:59 +08:00
|
|
|
|
2016-08-20 08:17:28 +08:00
|
|
|
namespace Wox.Plugin.Program.Programs
|
2016-08-20 06:05:59 +08:00
|
|
|
{
|
|
|
|
[Serializable]
|
2016-08-22 09:21:22 +08:00
|
|
|
public class Win32 : IProgram
|
2016-08-20 06:05:59 +08:00
|
|
|
{
|
2016-08-21 01:50:14 +08:00
|
|
|
public string Name { get; set; }
|
2019-09-10 05:57:03 +08:00
|
|
|
public string UniqueIdentifier { get; set; }
|
2016-08-20 08:17:28 +08:00
|
|
|
public string IcoPath { get; set; }
|
2016-08-21 00:08:16 +08:00
|
|
|
public string FullPath { get; set; }
|
|
|
|
public string ParentDirectory { get; set; }
|
2016-08-20 08:17:28 +08:00
|
|
|
public string ExecutableName { get; set; }
|
2016-08-21 01:50:14 +08:00
|
|
|
public string Description { get; set; }
|
2016-09-06 01:08:30 +08:00
|
|
|
public bool Valid { get; set; }
|
2019-09-08 20:18:55 +08:00
|
|
|
public bool Enabled { get; set; }
|
2019-10-17 19:47:00 +08:00
|
|
|
public string Location => ParentDirectory;
|
2016-08-20 06:05:59 +08:00
|
|
|
|
2016-08-21 00:08:16 +08:00
|
|
|
private const string ShortcutExtension = "lnk";
|
|
|
|
private const string ApplicationReferenceExtension = "appref-ms";
|
|
|
|
private const string ExeExtension = "exe";
|
|
|
|
|
2016-08-22 09:21:22 +08:00
|
|
|
private int Score(string query)
|
|
|
|
{
|
2019-09-29 13:03:30 +08:00
|
|
|
var score1 = StringMatcher.FuzzySearch(query, Name).ScoreAfterSearchPrecisionFilter();
|
2016-08-22 09:21:22 +08:00
|
|
|
var score2 = StringMatcher.ScoreForPinyin(Name, query);
|
2019-09-29 13:03:30 +08:00
|
|
|
var score3 = StringMatcher.FuzzySearch(query, Description).ScoreAfterSearchPrecisionFilter();
|
2016-08-22 09:21:22 +08:00
|
|
|
var score4 = StringMatcher.ScoreForPinyin(Description, query);
|
2019-09-29 13:03:30 +08:00
|
|
|
var score5 = StringMatcher.FuzzySearch(query, ExecutableName).ScoreAfterSearchPrecisionFilter();
|
2016-08-22 09:21:22 +08:00
|
|
|
var score = new[] { score1, score2, score3, score4, score5 }.Max();
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Result Result(string query, IPublicAPI api)
|
|
|
|
{
|
2019-11-29 07:38:50 +08:00
|
|
|
var score = Score(query);
|
|
|
|
if (score <= 0)
|
|
|
|
{ // no need to create result if this is zero
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-08-22 09:21:22 +08:00
|
|
|
var result = new Result
|
|
|
|
{
|
|
|
|
SubTitle = FullPath,
|
|
|
|
IcoPath = IcoPath,
|
2019-11-29 07:38:50 +08:00
|
|
|
Score = score,
|
2016-08-22 09:21:22 +08:00
|
|
|
ContextData = this,
|
|
|
|
Action = e =>
|
|
|
|
{
|
|
|
|
var info = new ProcessStartInfo
|
|
|
|
{
|
|
|
|
FileName = FullPath,
|
|
|
|
WorkingDirectory = ParentDirectory
|
|
|
|
};
|
|
|
|
var hide = Main.StartProcess(info);
|
|
|
|
return hide;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Description.Length >= Name.Length &&
|
|
|
|
Description.Substring(0, Name.Length) == Name)
|
|
|
|
{
|
|
|
|
result.Title = Description;
|
|
|
|
}
|
|
|
|
else if (!string.IsNullOrEmpty(Description))
|
|
|
|
{
|
|
|
|
result.Title = $"{Name}: {Description}";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.Title = Name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<Result> ContextMenus(IPublicAPI api)
|
|
|
|
{
|
|
|
|
var contextMenus = new List<Result>
|
|
|
|
{
|
|
|
|
new Result
|
|
|
|
{
|
|
|
|
Title = api.GetTranslation("wox_plugin_program_run_as_administrator"),
|
|
|
|
Action = _ =>
|
|
|
|
{
|
|
|
|
var info = new ProcessStartInfo
|
|
|
|
{
|
|
|
|
FileName = FullPath,
|
|
|
|
WorkingDirectory = ParentDirectory,
|
|
|
|
Verb = "runas"
|
|
|
|
};
|
|
|
|
var hide = Main.StartProcess(info);
|
|
|
|
return hide;
|
|
|
|
},
|
|
|
|
IcoPath = "Images/cmd.png"
|
|
|
|
},
|
|
|
|
new Result
|
|
|
|
{
|
|
|
|
Title = api.GetTranslation("wox_plugin_program_open_containing_folder"),
|
|
|
|
Action = _ =>
|
|
|
|
{
|
|
|
|
var hide = Main.StartProcess(new ProcessStartInfo(ParentDirectory));
|
|
|
|
return hide;
|
|
|
|
},
|
|
|
|
IcoPath = "Images/folder.png"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return contextMenus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-21 00:08:16 +08:00
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return ExecutableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Win32 Win32Program(string path)
|
2016-08-20 06:05:59 +08:00
|
|
|
{
|
2019-10-25 10:11:52 +08:00
|
|
|
try
|
2016-08-20 06:05:59 +08:00
|
|
|
{
|
2019-10-25 10:11:52 +08:00
|
|
|
var p = new Win32
|
|
|
|
{
|
|
|
|
Name = Path.GetFileNameWithoutExtension(path),
|
|
|
|
IcoPath = path,
|
|
|
|
FullPath = path,
|
|
|
|
UniqueIdentifier = path,
|
|
|
|
ParentDirectory = Directory.GetParent(path).FullName,
|
|
|
|
Description = string.Empty,
|
|
|
|
Valid = true,
|
|
|
|
Enabled = true
|
|
|
|
};
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
|
|
|
{
|
|
|
|
ProgramLogger.LogException($"|Win32|Win32Program|{path}" +
|
|
|
|
$"|Permission denied when trying to load the program from {path}", e);
|
|
|
|
|
|
|
|
return new Win32() { Valid = false, Enabled = false };
|
|
|
|
}
|
2016-08-21 00:08:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Win32 LnkProgram(string path)
|
|
|
|
{
|
|
|
|
var program = Win32Program(path);
|
|
|
|
try
|
2016-08-20 06:05:59 +08:00
|
|
|
{
|
2016-08-21 01:18:41 +08:00
|
|
|
var link = new ShellLink();
|
|
|
|
const uint STGM_READ = 0;
|
2017-01-24 08:24:20 +08:00
|
|
|
((IPersistFile)link).Load(path, STGM_READ);
|
2016-08-21 01:18:41 +08:00
|
|
|
var hwnd = new _RemotableHandle();
|
|
|
|
link.Resolve(ref hwnd, 0);
|
|
|
|
|
|
|
|
const int MAX_PATH = 260;
|
|
|
|
StringBuilder buffer = new StringBuilder(MAX_PATH);
|
2016-09-06 01:08:30 +08:00
|
|
|
|
|
|
|
var data = new _WIN32_FIND_DATAW();
|
|
|
|
const uint SLGP_SHORTPATH = 1;
|
|
|
|
link.GetPath(buffer, buffer.Capacity, ref data, SLGP_SHORTPATH);
|
|
|
|
var target = buffer.ToString();
|
2016-11-29 09:55:25 +08:00
|
|
|
if (!string.IsNullOrEmpty(target))
|
2016-08-21 00:08:16 +08:00
|
|
|
{
|
2016-11-29 09:55:25 +08:00
|
|
|
var extension = Extension(target);
|
|
|
|
if (extension == ExeExtension && File.Exists(target))
|
2016-09-06 01:08:30 +08:00
|
|
|
{
|
2016-11-29 09:55:25 +08:00
|
|
|
buffer = new StringBuilder(MAX_PATH);
|
|
|
|
link.GetDescription(buffer, MAX_PATH);
|
|
|
|
var description = buffer.ToString();
|
|
|
|
if (!string.IsNullOrEmpty(description))
|
|
|
|
{
|
|
|
|
program.Description = description;
|
|
|
|
}
|
|
|
|
else
|
2016-08-20 06:05:59 +08:00
|
|
|
{
|
2016-11-29 09:55:25 +08:00
|
|
|
var info = FileVersionInfo.GetVersionInfo(target);
|
|
|
|
if (!string.IsNullOrEmpty(info.FileDescription))
|
|
|
|
{
|
|
|
|
program.Description = info.FileDescription;
|
|
|
|
}
|
2016-08-20 06:05:59 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-21 00:08:16 +08:00
|
|
|
}
|
|
|
|
return program;
|
|
|
|
}
|
2016-11-29 09:55:25 +08:00
|
|
|
catch (COMException e)
|
|
|
|
{
|
|
|
|
// C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\MiracastView.lnk always cause exception
|
2019-11-29 07:38:50 +08:00
|
|
|
ProgramLogger.LogException($"|Win32|LnkProgram|{path}" +
|
2019-10-25 10:11:52 +08:00
|
|
|
"|Error caused likely due to trying to get the description of the program", e);
|
|
|
|
|
2016-11-29 09:55:25 +08:00
|
|
|
program.Valid = false;
|
|
|
|
return program;
|
|
|
|
}
|
2019-10-25 10:11:52 +08:00
|
|
|
#if !DEBUG //Only do a catch all in production. This is so make developer aware of any unhandled exception and add the exception handling in.
|
2016-11-29 09:55:25 +08:00
|
|
|
catch (Exception e)
|
2016-08-21 00:08:16 +08:00
|
|
|
{
|
2019-10-25 10:11:52 +08:00
|
|
|
ProgramLogger.LogException($"|Win32|LnkProgram|{path}" +
|
|
|
|
"|An unexpected error occurred in the calling method LnkProgram", e);
|
|
|
|
|
2016-11-29 09:55:25 +08:00
|
|
|
program.Valid = false;
|
2016-08-21 00:08:16 +08:00
|
|
|
return program;
|
2016-08-20 06:05:59 +08:00
|
|
|
}
|
2019-10-25 10:11:52 +08:00
|
|
|
#endif
|
2016-08-20 06:05:59 +08:00
|
|
|
}
|
|
|
|
|
2016-08-21 00:08:16 +08:00
|
|
|
private static Win32 ExeProgram(string path)
|
2016-08-20 06:05:59 +08:00
|
|
|
{
|
2019-10-25 10:11:52 +08:00
|
|
|
try
|
2016-08-20 06:05:59 +08:00
|
|
|
{
|
2019-10-25 10:11:52 +08:00
|
|
|
var program = Win32Program(path);
|
|
|
|
var info = FileVersionInfo.GetVersionInfo(path);
|
|
|
|
if (!string.IsNullOrEmpty(info.FileDescription))
|
|
|
|
{
|
|
|
|
program.Description = info.FileDescription;
|
|
|
|
}
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
|
|
|
{
|
|
|
|
ProgramLogger.LogException($"|Win32|ExeProgram|{path}" +
|
|
|
|
$"|Permission denied when trying to load the program from {path}", e);
|
|
|
|
|
|
|
|
return new Win32() { Valid = false, Enabled = false };
|
2016-08-20 08:17:28 +08:00
|
|
|
}
|
2016-08-21 00:08:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static IEnumerable<string> ProgramPaths(string directory, string[] suffixes)
|
|
|
|
{
|
2017-03-08 09:44:04 +08:00
|
|
|
if (!Directory.Exists(directory))
|
|
|
|
return new string[] { };
|
2017-12-24 18:52:53 +08:00
|
|
|
var files = new List<string>();
|
|
|
|
var folderQueue = new Queue<string>();
|
|
|
|
folderQueue.Enqueue(directory);
|
|
|
|
do
|
2016-08-21 00:08:16 +08:00
|
|
|
{
|
2017-12-24 18:52:53 +08:00
|
|
|
var currentDirectory = folderQueue.Dequeue();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
foreach (var suffix in suffixes)
|
|
|
|
{
|
2018-07-15 11:28:01 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
files.AddRange(Directory.EnumerateFiles(currentDirectory, $"*.{suffix}", SearchOption.TopDirectoryOnly));
|
|
|
|
}
|
|
|
|
catch (DirectoryNotFoundException e)
|
|
|
|
{
|
2019-10-25 10:11:52 +08:00
|
|
|
ProgramLogger.LogException($"|Win32|ProgramPaths|{currentDirectory}" +
|
|
|
|
"|The directory trying to load the program from does not exist", e);
|
2018-07-15 11:28:01 +08:00
|
|
|
}
|
2017-12-24 18:52:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
|
|
|
{
|
2019-10-25 10:11:52 +08:00
|
|
|
ProgramLogger.LogException($"|Win32|ProgramPaths|{currentDirectory}" +
|
|
|
|
$"|Permission denied when trying to load programs from {currentDirectory}", e);
|
2017-12-24 18:52:53 +08:00
|
|
|
}
|
|
|
|
|
2017-03-02 07:21:34 +08:00
|
|
|
try
|
|
|
|
{
|
2017-12-24 18:52:53 +08:00
|
|
|
foreach (var childDirectory in Directory.EnumerateDirectories(currentDirectory, "*", SearchOption.TopDirectoryOnly))
|
2017-03-08 09:44:04 +08:00
|
|
|
{
|
2017-12-24 18:52:53 +08:00
|
|
|
folderQueue.Enqueue(childDirectory);
|
|
|
|
}
|
2017-03-02 07:21:34 +08:00
|
|
|
}
|
|
|
|
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
|
|
|
{
|
2019-10-25 10:11:52 +08:00
|
|
|
ProgramLogger.LogException($"|Win32|ProgramPaths|{currentDirectory}" +
|
|
|
|
$"|Permission denied when trying to load programs from {currentDirectory}", e);
|
2017-03-02 07:21:34 +08:00
|
|
|
}
|
2017-12-24 18:52:53 +08:00
|
|
|
} while (folderQueue.Any());
|
|
|
|
return files;
|
2016-08-21 00:08:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static string Extension(string path)
|
|
|
|
{
|
|
|
|
var extension = Path.GetExtension(path)?.ToLower();
|
|
|
|
if (!string.IsNullOrEmpty(extension))
|
2016-08-20 08:17:28 +08:00
|
|
|
{
|
2016-08-21 00:08:16 +08:00
|
|
|
return extension.Substring(1);
|
2016-08-20 08:17:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-21 00:08:16 +08:00
|
|
|
return string.Empty;
|
2016-08-20 08:17:28 +08:00
|
|
|
}
|
2016-08-21 00:08:16 +08:00
|
|
|
}
|
2016-08-20 08:17:28 +08:00
|
|
|
|
2016-08-21 00:08:16 +08:00
|
|
|
private static ParallelQuery<Win32> UnregisteredPrograms(List<Settings.ProgramSource> sources, string[] suffixes)
|
|
|
|
{
|
2019-09-10 05:57:03 +08:00
|
|
|
var listToAdd = new List<string>();
|
2019-09-06 06:06:51 +08:00
|
|
|
sources.Where(s => Directory.Exists(s.Location) && s.Enabled)
|
2019-09-05 06:05:17 +08:00
|
|
|
.SelectMany(s => ProgramPaths(s.Location, suffixes))
|
|
|
|
.ToList()
|
2019-10-15 19:39:55 +08:00
|
|
|
.Where(t1 => !Main._settings.DisabledProgramSources.Any(x => t1 == x.UniqueIdentifier))
|
|
|
|
.ToList()
|
2019-09-10 05:57:03 +08:00
|
|
|
.ForEach(x => listToAdd.Add(x));
|
2019-09-05 06:05:17 +08:00
|
|
|
|
2019-10-24 09:51:33 +08:00
|
|
|
var paths = listToAdd.Distinct().ToArray();
|
2019-09-05 06:05:17 +08:00
|
|
|
|
2016-08-21 00:08:16 +08:00
|
|
|
var programs1 = paths.AsParallel().Where(p => Extension(p) == ExeExtension).Select(ExeProgram);
|
|
|
|
var programs2 = paths.AsParallel().Where(p => Extension(p) == ShortcutExtension).Select(ExeProgram);
|
|
|
|
var programs3 = from p in paths.AsParallel()
|
|
|
|
let e = Extension(p)
|
|
|
|
where e != ShortcutExtension && e != ExeExtension
|
|
|
|
select Win32Program(p);
|
|
|
|
return programs1.Concat(programs2).Concat(programs3);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ParallelQuery<Win32> StartMenuPrograms(string[] suffixes)
|
|
|
|
{
|
2019-10-15 17:20:55 +08:00
|
|
|
var disabledProgramsList = Main._settings.DisabledProgramSources;
|
2019-09-10 05:57:03 +08:00
|
|
|
|
2016-08-21 00:08:16 +08:00
|
|
|
var directory1 = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
|
|
|
|
var directory2 = Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms);
|
|
|
|
var paths1 = ProgramPaths(directory1, suffixes);
|
|
|
|
var paths2 = ProgramPaths(directory2, suffixes);
|
2019-09-10 05:57:03 +08:00
|
|
|
|
|
|
|
var toFilter = paths1.Concat(paths2);
|
2019-09-13 05:26:24 +08:00
|
|
|
var paths = toFilter
|
2019-10-15 17:20:55 +08:00
|
|
|
.Where(t1 => !disabledProgramsList.Any(x => x.UniqueIdentifier == t1))
|
2019-09-13 05:26:24 +08:00
|
|
|
.Select(t1 => t1)
|
2019-10-24 10:31:09 +08:00
|
|
|
.Distinct()
|
2019-09-13 05:26:24 +08:00
|
|
|
.ToArray();
|
2019-09-10 05:57:03 +08:00
|
|
|
|
2016-08-21 00:08:16 +08:00
|
|
|
var programs1 = paths.AsParallel().Where(p => Extension(p) == ShortcutExtension).Select(LnkProgram);
|
|
|
|
var programs2 = paths.AsParallel().Where(p => Extension(p) == ApplicationReferenceExtension).Select(Win32Program);
|
2016-09-06 01:08:30 +08:00
|
|
|
var programs = programs1.Concat(programs2).Where(p => p.Valid);
|
|
|
|
return programs;
|
2016-08-21 00:08:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static ParallelQuery<Win32> AppPathsPrograms(string[] suffixes)
|
|
|
|
{
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121
|
|
|
|
const string appPaths = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths";
|
|
|
|
var programs = new List<Win32>();
|
|
|
|
using (var root = Registry.LocalMachine.OpenSubKey(appPaths))
|
2016-08-20 08:17:28 +08:00
|
|
|
{
|
2016-08-21 00:08:16 +08:00
|
|
|
if (root != null)
|
2016-08-20 06:05:59 +08:00
|
|
|
{
|
2019-10-24 09:57:07 +08:00
|
|
|
programs.AddRange(GetProgramsFromRegistry(root));
|
2016-08-21 00:08:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
using (var root = Registry.CurrentUser.OpenSubKey(appPaths))
|
|
|
|
{
|
|
|
|
if (root != null)
|
|
|
|
{
|
2019-10-24 09:57:07 +08:00
|
|
|
programs.AddRange(GetProgramsFromRegistry(root));
|
2016-08-21 00:08:16 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-10 05:57:03 +08:00
|
|
|
|
2019-10-15 17:20:55 +08:00
|
|
|
var disabledProgramsList = Main._settings.DisabledProgramSources;
|
2019-09-10 05:57:03 +08:00
|
|
|
var toFilter = programs.AsParallel().Where(p => suffixes.Contains(Extension(p.ExecutableName)));
|
|
|
|
|
2019-10-15 17:20:55 +08:00
|
|
|
var filtered = toFilter.Where(t1 => !disabledProgramsList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier)).Select(t1 => t1);
|
2019-09-10 05:57:03 +08:00
|
|
|
|
2016-08-21 00:08:16 +08:00
|
|
|
return filtered;
|
|
|
|
}
|
|
|
|
|
2019-10-24 09:57:07 +08:00
|
|
|
private static IEnumerable<Win32> GetProgramsFromRegistry(RegistryKey root)
|
2016-08-21 00:08:16 +08:00
|
|
|
{
|
2019-10-24 09:42:35 +08:00
|
|
|
return root
|
|
|
|
.GetSubKeyNames()
|
2019-10-24 09:57:07 +08:00
|
|
|
.Select(x => GetProgramPathFromRegistrySubKeys(root, x))
|
2019-10-24 09:42:35 +08:00
|
|
|
.Distinct()
|
2019-10-24 09:57:07 +08:00
|
|
|
.Select(x => GetProgramFromPath(x));
|
2016-08-21 00:08:16 +08:00
|
|
|
}
|
|
|
|
|
2019-10-24 09:57:07 +08:00
|
|
|
private static string GetProgramPathFromRegistrySubKeys(RegistryKey root, string subkey)
|
2016-08-21 00:08:16 +08:00
|
|
|
{
|
2019-10-24 09:42:35 +08:00
|
|
|
var path = string.Empty;
|
2019-10-25 10:11:52 +08:00
|
|
|
try
|
2016-08-21 00:08:16 +08:00
|
|
|
{
|
2019-10-25 10:11:52 +08:00
|
|
|
using (var key = root.OpenSubKey(subkey))
|
|
|
|
{
|
|
|
|
if (key == null)
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
var defaultValue = string.Empty;
|
|
|
|
path = key.GetValue(defaultValue) as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
2019-10-24 09:42:35 +08:00
|
|
|
return string.Empty;
|
2016-08-21 00:08:16 +08:00
|
|
|
|
2019-10-25 10:11:52 +08:00
|
|
|
// fix path like this: ""\"C:\\folder\\executable.exe\""
|
|
|
|
return path = path.Trim('"', ' ');
|
2016-08-20 08:17:28 +08:00
|
|
|
}
|
2019-10-25 10:11:52 +08:00
|
|
|
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
|
|
|
{
|
|
|
|
ProgramLogger.LogException($"|Win32|GetProgramPathFromRegistrySubKeys|{path}" +
|
|
|
|
$"|Permission denied when trying to load the program from {path}", e);
|
2019-10-24 09:42:35 +08:00
|
|
|
|
|
|
|
return string.Empty;
|
2019-10-25 10:11:52 +08:00
|
|
|
}
|
2016-08-21 00:08:16 +08:00
|
|
|
}
|
|
|
|
|
2019-10-24 09:57:07 +08:00
|
|
|
private static Win32 GetProgramFromPath(string path)
|
2019-10-24 09:42:35 +08:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
return new Win32();
|
|
|
|
|
|
|
|
path = Environment.ExpandEnvironmentVariables(path);
|
|
|
|
|
|
|
|
if (!File.Exists(path))
|
|
|
|
return new Win32();
|
|
|
|
|
|
|
|
var entry = Win32Program(path);
|
|
|
|
entry.ExecutableName = Path.GetFileName(path);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
2016-08-21 00:08:16 +08:00
|
|
|
|
|
|
|
public static Win32[] All(Settings settings)
|
|
|
|
{
|
2019-10-25 10:11:52 +08:00
|
|
|
try
|
2016-08-21 00:08:16 +08:00
|
|
|
{
|
2019-10-25 10:11:52 +08:00
|
|
|
var programs = new List<Win32>().AsParallel();
|
|
|
|
|
|
|
|
var unregistered = UnregisteredPrograms(settings.ProgramSources, settings.ProgramSuffixes);
|
|
|
|
programs = programs.Concat(unregistered);
|
|
|
|
if (settings.EnableRegistrySource)
|
|
|
|
{
|
|
|
|
var appPaths = AppPathsPrograms(settings.ProgramSuffixes);
|
|
|
|
programs = programs.Concat(appPaths);
|
|
|
|
}
|
2019-09-05 06:05:17 +08:00
|
|
|
|
2019-10-25 10:11:52 +08:00
|
|
|
if (settings.EnableStartMenuSource)
|
|
|
|
{
|
|
|
|
var startMenu = StartMenuPrograms(settings.ProgramSuffixes);
|
|
|
|
programs = programs.Concat(startMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
return programs.ToArray();
|
|
|
|
}
|
|
|
|
#if DEBUG //This is to make developer aware of any unhandled exception and add in handling.
|
|
|
|
catch (Exception e)
|
2016-08-20 08:17:28 +08:00
|
|
|
{
|
2019-10-25 10:11:52 +08:00
|
|
|
throw e;
|
2019-10-24 10:32:12 +08:00
|
|
|
}
|
2019-10-25 10:11:52 +08:00
|
|
|
#endif
|
2019-10-24 10:32:12 +08:00
|
|
|
|
2019-10-25 10:11:52 +08:00
|
|
|
#if !DEBUG //Only do a catch all in production.
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
ProgramLogger.LogException("|Win32|All|Not available|An unexpected error occurred", e);
|
|
|
|
|
|
|
|
return new Win32[0];
|
|
|
|
}
|
|
|
|
#endif
|
2016-08-20 06:05:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|