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

62 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 01:32:33 +00:00
using Wox.Infrastructure.Exception;
2015-10-30 23:17:34 +00:00
using Wox.Infrastructure.Logger;
namespace Wox.Plugin.Program.ProgramSources
{
[Serializable]
public class FileSystemProgramSource : ProgramSource
{
public string Location { get; set; } = "";
2016-08-19 20:34:20 +01:00
public int MaxDepth { get; set; } = -1;
internal string[] Suffixes { get; set; } = { "" };
public override List<Program> LoadPrograms()
{
2016-08-19 20:34:20 +01:00
if (Directory.Exists(Location) && MaxDepth >= -1)
{
2016-08-19 20:34:20 +01:00
var apps = new List<Program>();
GetAppFromDirectory(apps, Location, 0);
return apps;
}
else
{
return new List<Program>();
}
}
2016-08-19 20:34:20 +01:00
private void GetAppFromDirectory(List<Program> apps, string path, int depth)
{
2016-08-19 20:34:20 +01:00
if (MaxDepth != depth)
{
foreach (var file in Directory.GetFiles(path))
{
if (Suffixes.Any(o => file.EndsWith("." + o)))
{
2016-08-19 20:34:20 +01:00
Program p;
try
{
p = CreateEntry(file);
}
catch (Exception e)
{
var woxPluginException = new WoxPluginException("Program",
$"GetAppFromDirectory failed: {path}", e);
Log.Exception(woxPluginException);
continue;
}
p.Source = this;
2016-08-19 20:34:20 +01:00
apps.Add(p);
}
}
2016-08-19 20:34:20 +01:00
foreach (var d in Directory.GetDirectories(path))
{
2016-08-19 20:34:20 +01:00
GetAppFromDirectory(apps, d, depth + 1);
}
}
2014-03-19 20:16:20 +08:00
}
}
}