PowerToys/Plugins/Wox.Plugin.Program/Programs/AppPathsPrograms.cs
2016-08-20 01:17:28 +01:00

68 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Win32;
namespace Wox.Plugin.Program.Programs
{
[Serializable]
public class AppPathsPrograms : Win32
{
public static IEnumerable<Win32> All()
{
// https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121
var programs = new List<Win32>();
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;
}
private static IEnumerable<Win32> ProgramsFromRegistryKey(RegistryKey root)
{
var programs = root.GetSubKeyNames()
.Select(subkey => ProgramFromRegistrySubkey(root, subkey))
.Where(p => !string.IsNullOrEmpty(p.Title));
return programs;
}
private static Win32 ProgramFromRegistrySubkey(RegistryKey root, string subkey)
{
using (var key = root.OpenSubKey(subkey))
{
if (key != null)
{
var defaultValue = string.Empty;
var path = key.GetValue(defaultValue) as string;
if (!string.IsNullOrEmpty(path))
{
// fix path like this: ""\"C:\\folder\\executable.exe\""
path = path.Trim('"');
path = Environment.ExpandEnvironmentVariables(path);
if (File.Exists(path))
{
var entry = CreateEntry(path);
entry.ExecutableName = subkey;
return entry;
}
}
}
}
return new Win32();
}
}
}