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

66 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
2014-03-30 11:16:44 +08:00
using System.Linq;
2015-11-09 09:32:33 +08:00
using Wox.Infrastructure.Exception;
2015-10-31 07:17:34 +08:00
using Wox.Infrastructure.Logger;
namespace Wox.Plugin.Program.ProgramSources
{
[Serializable]
public class FileSystemProgramSource : ProgramSource
{
public string Location { get; set; } = "";
public override List<Program> LoadPrograms()
{
var list = new List<Program>();
if (Directory.Exists(Location))
{
GetAppFromDirectory(Location, list);
FileChangeWatcher.AddWatch(Location, Suffixes);
}
return list;
}
private void GetAppFromDirectory(string path, List<Program> list)
{
GetAppFromDirectory(path, list, 0);
}
private void GetAppFromDirectory(string path, List<Program> list, int depth)
{
if (MaxDepth != -1 && depth > MaxDepth)
{
return;
}
try
{
foreach (var file in Directory.GetFiles(path))
{
if (Suffixes.Any(o => file.EndsWith("." + o)))
{
var p = CreateEntry(file);
p.Source = this;
list.Add(p);
}
}
foreach (var subDirectory in Directory.GetDirectories(path))
{
GetAppFromDirectory(subDirectory, list, depth + 1);
}
}
2015-02-02 23:28:40 +08:00
catch (Exception e)
{
var woxPluginException = new WoxPluginException("Program", $"GetAppFromDirectory failed: {path}", e);
2016-05-16 00:03:06 +08:00
Log.Exception(woxPluginException);
}
}
2014-03-19 20:16:20 +08:00
public override string ToString()
{
var display = GetType().Name + Location;
return display;
2014-03-19 20:16:20 +08:00
}
}
}