-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirFilesCache.cs
67 lines (59 loc) · 2.04 KB
/
DirFilesCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Main website for TVRename is http://tvrename.com
//
// Source code available at https://github.com/TV-Rename/tvrename
//
// Copyright (c) TV Rename. This code is released under GPLv3 https://github.com/TV-Rename/tvrename/blob/master/LICENSE.md
//
using System;
using System.Collections.Generic;
using Alphaleonis.Win32.Filesystem;
using JetBrains.Annotations;
// Will cache the file lists of contents of single directories. Will return the cached
// data, or read cache and return it.
namespace TVRename
{
public class DirFilesCache
{
private readonly Dictionary<string, FileInfo[]> cache = new Dictionary<string, FileInfo[]>();
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
public FileInfo[] GetFilesIncludeSubDirs([NotNull] string folder) => Get(folder, true);
public FileInfo[] GetFiles([NotNull] string folder) => Get(folder, false);
private FileInfo[] Get([NotNull] string folder,bool includeSubs)
{
if (cache.ContainsKey(folder))
{
return cache[folder];
}
DirectoryInfo di;
try
{
di = new DirectoryInfo(folder);
}
catch
{
cache[folder] = null;
return null;
}
if (!di.Exists)
{
cache[folder] = null;
return null;
}
try {
FileInfo[] files = includeSubs ? di.GetFiles("*", System.IO.SearchOption.AllDirectories): di.GetFiles();
cache[folder] = files;
return files;
}
catch (System.IO.IOException) {
Logger.Warn("IOException occurred trying to access " + folder);
return null;
}
catch (UnauthorizedAccessException)
{
Logger.Warn("IOException occurred trying to access " + folder);
return null;
}
}
}
}