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;
2014-09-19 16:57:48 +08:00
using Log = Wox.Infrastructure.Logger.Log;
namespace Wox.Plugin.Program.ProgramSources
{
[Serializable]
public class FileSystemProgramSource : AbstractProgramSource
{
private string baseDirectory;
public FileSystemProgramSource(string baseDirectory)
{
this.baseDirectory = baseDirectory;
}
2015-02-02 23:28:40 +08:00
public FileSystemProgramSource(ProgramSource source)
: this(source.Location)
{
2014-03-19 20:16:20 +08:00
this.BonusPoints = source.BonusPoints;
}
public override List<Program> LoadPrograms()
{
List<Program> list = new List<Program>();
if (Directory.Exists(baseDirectory))
{
GetAppFromDirectory(baseDirectory, list);
FileChangeWatcher.AddWatch(baseDirectory);
}
return list;
}
private void GetAppFromDirectory(string path, List<Program> list)
{
try
{
foreach (string file in Directory.GetFiles(path))
{
if (ProgramStorage.Instance.ProgramSuffixes.Split(';').Any(o => file.EndsWith("." + o)))
{
Program p = CreateEntry(file);
list.Add(p);
}
}
foreach (var subDirectory in Directory.GetDirectories(path))
{
GetAppFromDirectory(subDirectory, list);
}
}
2015-02-02 23:28:40 +08:00
catch (Exception e)
{
2015-02-02 23:28:40 +08:00
Log.Warn(string.Format("GetAppFromDirectory failed: {0} - {1}", path, e.Message));
}
}
2014-03-19 20:16:20 +08:00
public override string ToString()
{
return typeof(FileSystemProgramSource).Name + ":" + this.baseDirectory;
2014-03-19 20:16:20 +08:00
}
}
}