2014-12-15 22:58:49 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2016-01-07 05:34:42 +08:00
|
|
|
|
using System.IO;
|
2016-08-20 05:08:45 +08:00
|
|
|
|
using System.Linq;
|
2016-01-07 05:34:42 +08:00
|
|
|
|
using Microsoft.Win32;
|
2014-03-19 02:06:51 +08:00
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
namespace Wox.Plugin.Program.ProgramSources
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2014-12-15 22:58:49 +08:00
|
|
|
|
[Serializable]
|
2016-08-20 06:05:59 +08:00
|
|
|
|
public class AppPathsProgramSource : Win32
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
|
|
|
|
public override List<Program> LoadPrograms()
|
|
|
|
|
{
|
2016-08-20 05:08:45 +08:00
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121
|
|
|
|
|
var programs = new List<Program>();
|
|
|
|
|
const string appPaths = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths";
|
|
|
|
|
using (var root = Registry.LocalMachine.OpenSubKey(appPaths))
|
|
|
|
|
{
|
|
|
|
|
if (root != null)
|
|
|
|
|
{
|
|
|
|
|
programs.AddRange(ProgramsFromRegistryKey(root));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
using (var root = Registry.CurrentUser.OpenSubKey(appPaths))
|
|
|
|
|
{
|
|
|
|
|
if (root != null)
|
|
|
|
|
{
|
|
|
|
|
programs.AddRange(ProgramsFromRegistryKey(root));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return programs;
|
2014-03-19 02:06:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-20 05:08:45 +08:00
|
|
|
|
private IEnumerable<Program> ProgramsFromRegistryKey(RegistryKey root)
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2016-08-20 05:08:45 +08:00
|
|
|
|
var programs = root.GetSubKeyNames()
|
|
|
|
|
.Select(subkey => ProgramFromRegistrySubkey(root, subkey))
|
|
|
|
|
.Where(p => !string.IsNullOrEmpty(p.Title));
|
|
|
|
|
return programs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Program ProgramFromRegistrySubkey(RegistryKey root, string subkey)
|
|
|
|
|
{
|
|
|
|
|
using (var key = root.OpenSubKey(subkey))
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2016-08-20 05:08:45 +08:00
|
|
|
|
if (key != null)
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2016-08-20 05:08:45 +08:00
|
|
|
|
var defaultValue = string.Empty;
|
|
|
|
|
var path = key.GetValue(defaultValue) as string;
|
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
2014-03-19 02:06:51 +08:00
|
|
|
|
{
|
2016-08-20 05:08:45 +08:00
|
|
|
|
// fix path like this: ""\"C:\\folder\\executable.exe\""
|
|
|
|
|
path = path.Trim('"');
|
|
|
|
|
path = Environment.ExpandEnvironmentVariables(path);
|
2015-11-01 06:40:11 +08:00
|
|
|
|
|
2016-08-20 05:08:45 +08:00
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
{
|
2015-11-01 06:40:11 +08:00
|
|
|
|
var entry = CreateEntry(path);
|
2016-08-20 05:08:45 +08:00
|
|
|
|
entry.ExecutableName = subkey;
|
|
|
|
|
return entry;
|
2014-03-20 04:15:05 +08:00
|
|
|
|
}
|
2015-11-01 06:40:11 +08:00
|
|
|
|
}
|
2014-03-19 02:06:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-20 05:08:45 +08:00
|
|
|
|
return new Program();
|
2014-03-19 02:06:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-22 02:51:47 +08:00
|
|
|
|
}
|