PowerToys/Plugins/Wox.Plugin.Program/ProgramSources/AppPathsProgramSource.cs

69 lines
2.4 KiB
C#
Raw Normal View History

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;
namespace Wox.Plugin.Program.ProgramSources
{
[Serializable]
public class AppPathsProgramSource : ProgramSource
{
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;
}
2016-08-20 05:08:45 +08:00
private IEnumerable<Program> ProgramsFromRegistryKey(RegistryKey root)
{
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))
{
2016-08-20 05:08:45 +08:00
if (key != null)
{
2016-08-20 05:08:45 +08:00
var defaultValue = string.Empty;
var path = key.GetValue(defaultValue) as string;
if (!string.IsNullOrEmpty(path))
{
2016-08-20 05:08:45 +08:00
// fix path like this: ""\"C:\\folder\\executable.exe\""
path = path.Trim('"');
path = Environment.ExpandEnvironmentVariables(path);
2016-08-20 05:08:45 +08:00
if (File.Exists(path))
{
var entry = CreateEntry(path);
2016-08-20 05:08:45 +08:00
entry.ExecutableName = subkey;
entry.Source = this;
2016-08-20 05:08:45 +08:00
return entry;
2014-03-20 04:15:05 +08:00
}
}
}
}
2016-08-20 05:08:45 +08:00
return new Program();
}
}
}