mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Refactoring App Paths source
This commit is contained in:
parent
8764aa9829
commit
f8cc54d4f2
1 changed files with 41 additions and 31 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
using Wox.Infrastructure.Logger;
|
|
||||||
|
|
||||||
namespace Wox.Plugin.Program.ProgramSources
|
namespace Wox.Plugin.Program.ProgramSources
|
||||||
{
|
{
|
||||||
|
|
@ -11,49 +11,59 @@ namespace Wox.Plugin.Program.ProgramSources
|
||||||
{
|
{
|
||||||
public override List<Program> LoadPrograms()
|
public override List<Program> LoadPrograms()
|
||||||
{
|
{
|
||||||
var list = new List<Program>();
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121
|
||||||
ReadAppPaths(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths", list);
|
var programs = new List<Program>();
|
||||||
ReadAppPaths(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\App Paths", list);
|
const string appPaths = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths";
|
||||||
//TODO: need test more on 64-bit
|
using (var root = Registry.LocalMachine.OpenSubKey(appPaths))
|
||||||
return list;
|
{
|
||||||
|
if (root != null)
|
||||||
|
{
|
||||||
|
programs.AddRange(ProgramsFromRegistryKey(root));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var root = Registry.CurrentUser.OpenSubKey(appPaths))
|
||||||
|
{
|
||||||
|
if (root != null)
|
||||||
|
{
|
||||||
|
programs.AddRange(ProgramsFromRegistryKey(root));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return programs;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ReadAppPaths(string rootpath, List<Program> list)
|
private IEnumerable<Program> ProgramsFromRegistryKey(RegistryKey root)
|
||||||
{
|
{
|
||||||
using (var root = Registry.LocalMachine.OpenSubKey(rootpath))
|
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))
|
||||||
{
|
{
|
||||||
if (root == null) return;
|
if (key != null)
|
||||||
foreach (var item in root.GetSubKeyNames())
|
|
||||||
{
|
{
|
||||||
try
|
var defaultValue = string.Empty;
|
||||||
|
var path = key.GetValue(defaultValue) as string;
|
||||||
|
if (!string.IsNullOrEmpty(path))
|
||||||
{
|
{
|
||||||
using (var key = root.OpenSubKey(item))
|
// fix path like this: ""\"C:\\folder\\executable.exe\""
|
||||||
|
path = path.Trim('"');
|
||||||
|
path = Environment.ExpandEnvironmentVariables(path);
|
||||||
|
|
||||||
|
if (File.Exists(path))
|
||||||
{
|
{
|
||||||
var path = key.GetValue("") as string;
|
|
||||||
if (string.IsNullOrEmpty(path)) continue;
|
|
||||||
|
|
||||||
// fix path like this ""\"C:\\folder\\executable.exe\"""
|
|
||||||
const int begin = 0;
|
|
||||||
var end = path.Length - 1;
|
|
||||||
const char quotationMark = '"';
|
|
||||||
if (path[begin] == quotationMark && path[end] == quotationMark)
|
|
||||||
{
|
|
||||||
path = path.Substring(begin + 1, path.Length - 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!File.Exists(path)) continue;
|
|
||||||
var entry = CreateEntry(path);
|
var entry = CreateEntry(path);
|
||||||
entry.ExecutableName = item;
|
entry.ExecutableName = subkey;
|
||||||
entry.Source = this;
|
entry.Source = this;
|
||||||
list.Add(entry);
|
return entry;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Log.Exception(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return new Program();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue