forked from rickparrish/RMLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectoryTreeThread.cs
139 lines (118 loc) · 5.42 KB
/
DirectoryTreeThread.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
RMLib: Nonvisual support classes used by multiple R&M Software programs
Copyright (C) Rick Parrish, R&M Software
This file is part of RMLib.
RMLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RMLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with RMLib. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using RandM.RMLib;
using System.IO;
using System.Text.RegularExpressions;
namespace RandM.RMLib
{
public class DirectoryTreeThread : RMThread
{
public event EventHandler<StringEventArgs> DirectoryChanged = null;
public event EventHandler<StringEventArgs> LogError = null;
public event EventHandler<StringEventArgs> LogMessage = null;
public SortedDictionary<string, string> Directories { get; private set; }
public SortedDictionary<string, FileData> Files { get; private set; }
private string _Directory = null;
private string _ReplaceInKey = null;
private bool _Recurse = false;
private bool _WantFiles = false;
public DirectoryTreeThread(string directory, string replaceInKey, bool recurse, bool wantFiles)
{
_Directory = directory;
if (!_Directory.EndsWith(Path.DirectorySeparatorChar.ToString())) _Directory += Path.DirectorySeparatorChar;
_ReplaceInKey = Regex.Escape(replaceInKey);
_Recurse = recurse;
_WantFiles = wantFiles;
Directories = new SortedDictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
Files = new SortedDictionary<string, FileData>(StringComparer.CurrentCultureIgnoreCase);
}
protected override void Execute()
{
List<DirectoryInfo> ToSearch = new List<DirectoryInfo>();
// Add directory to queue
try
{
ToSearch.Add(new DirectoryInfo(_Directory));
}
catch (DirectoryNotFoundException dnf)
{
RaiseLogMessage("HANDLED DIRECTORYNOTFOUNDEXCEPTION while trying to read directory '" + _Directory + "': " + dnf.Message);
}
catch (Exception e)
{
RaiseLogError("UNHANDLED EXCEPTION while trying to read directory '" + _Directory + "': " + e.Message);
}
while (ToSearch.Count > 0)
{
DirectoryInfo DI = ToSearch[0];
ToSearch.RemoveAt(0);
Directories.Add(Regex.Replace(DI.FullName, _ReplaceInKey, "", RegexOptions.IgnoreCase), DI.FullName);
RaiseDirectoryChanged(DI.FullName);
if (_WantFiles)
{
try
{
foreach (FileData FD in FastDirectoryEnumerator.EnumerateFiles(DI.FullName, "*.*", _Recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
{
Files.Add(Regex.Replace(FD.Path, _ReplaceInKey, "", RegexOptions.IgnoreCase), FD);
}
}
catch (DirectoryNotFoundException dnf)
{
RaiseLogMessage("HANDLED DIRECTORYNOTFOUNDEXCEPTION while trying to read files in directory '" + DI.FullName + "': " + dnf.Message);
}
catch (Exception e)
{
RaiseLogError("UNHANDLED EXCEPTION while trying to read files in directory '" + DI.FullName + "': " + e.Message);
}
}
if (_Recurse)
{
try
{
ToSearch.AddRange(DI.GetDirectories());
}
catch (DirectoryNotFoundException dnf)
{
RaiseLogMessage("HANDLED DIRECTORYNOTFOUNDEXCEPTION while trying to read subdirectories in directory '" + DI.FullName + "': " + dnf.Message);
}
catch (Exception e)
{
RaiseLogError("UNHANDLED EXCEPTION while trying to read subdirectories in directory '" + DI.FullName + "': " + e.Message);
}
}
}
}
private void RaiseDirectoryChanged(string newDirectory)
{
EventHandler<StringEventArgs> Handler = DirectoryChanged;
if (Handler != null) Handler(this, new StringEventArgs(Regex.Replace(newDirectory, _ReplaceInKey, "", RegexOptions.IgnoreCase)));
}
private void RaiseLogError(string message)
{
EventHandler<StringEventArgs> Handler = LogError;
if (Handler != null) Handler(this, new StringEventArgs(message));
}
private void RaiseLogMessage(string message)
{
EventHandler<StringEventArgs> Handler = LogMessage;
if (Handler != null) Handler(this, new StringEventArgs(message));
}
}
}